/
newgame.cc
2361 lines (2068 loc) · 71.4 KB
/
newgame.cc
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
/**
* @file
* @brief Functions used when starting a new game.
**/
#include "AppHdr.h"
#include "newgame.h"
#include "cio.h"
#include "command.h"
#include "database.h"
#include "end.h"
#include "english.h"
#include "files.h"
#include "hints.h"
#include "initfile.h"
#include "item-name.h" // make_name
#include "item-prop.h"
#include "jobs.h"
#include "libutil.h"
#include "macro.h"
#include "maps.h"
#include "menu.h"
#include "ng-input.h"
#include "ng-restr.h"
#include "options.h"
#include "playable.h"
#include "prompt.h"
#include "skills.h"
#include "species-groups.h"
#include "state.h"
#include "stringutil.h"
#include "tilepick.h"
#ifdef USE_TILE
#include "tilereg-crt.h"
#include "tilepick-p.h"
#include "tilefont.h"
#include "rltiles/tiledef-main.h"
#include "rltiles/tiledef-feat.h"
#endif
#include "version.h"
#include "ui.h"
#include "outer-menu.h"
using namespace ui;
static void _choose_gamemode_map(newgame_def& ng, newgame_def& ng_choice,
const newgame_def& defaults);
static bool _choose_weapon(newgame_def& ng, newgame_def& ng_choice,
const newgame_def& defaults);
#ifdef USE_TILE_LOCAL
# define STARTUP_HIGHLIGHT_NORMAL LIGHTGRAY
# define STARTUP_HIGHLIGHT_BAD LIGHTGRAY
# define STARTUP_HIGHLIGHT_CONTROL LIGHTGRAY
# define STARTUP_HIGHLIGHT_GOOD LIGHTGREEN
#else
# define STARTUP_HIGHLIGHT_NORMAL BLUE
# define STARTUP_HIGHLIGHT_BAD LIGHTGRAY
# define STARTUP_HIGHLIGHT_CONTROL BLUE
# define STARTUP_HIGHLIGHT_GOOD GREEN
#endif
////////////////////////////////////////////////////////////////////////
// Remember player's startup options
//
newgame_def::newgame_def()
: name(), type(GAME_TYPE_NORMAL),
seed(0), pregenerate(false),
species(SP_UNKNOWN), job(JOB_UNKNOWN),
weapon(WPN_UNKNOWN),
fully_random(false)
{
}
void newgame_def::clear_character()
{
species = SP_UNKNOWN;
job = JOB_UNKNOWN;
weapon = WPN_UNKNOWN;
}
enum MenuOptions
{
M_QUIT = -1,
M_ABORT = -2,
M_APTITUDES = -3,
M_HELP = -4,
M_VIABLE = -5,
M_RANDOM = -6,
M_VIABLE_CHAR = -7,
M_RANDOM_CHAR = -8,
M_DEFAULT_CHOICE = -9,
};
enum MenuChoices
{
C_SPECIES = 0,
C_JOB = 1,
};
enum MenuItemStatuses
{
ITEM_STATUS_UNKNOWN,
ITEM_STATUS_RESTRICTED,
ITEM_STATUS_ALLOWED
};
static bool _is_random_species(species_type sp)
{
return sp == SP_RANDOM || sp == SP_VIABLE;
}
static bool _is_random_job(job_type job)
{
return job == JOB_RANDOM || job == JOB_VIABLE;
}
static bool _is_random_choice(const newgame_def& choice)
{
return _is_random_species(choice.species)
&& _is_random_job(choice.job);
}
static bool _is_random_viable_choice(const newgame_def& choice)
{
return _is_random_choice(choice) &&
(choice.job == JOB_VIABLE || choice.species == SP_VIABLE);
}
static bool _char_defined(const newgame_def& ng)
{
return ng.species != SP_UNKNOWN && ng.job != JOB_UNKNOWN;
}
static char_choice_restriction _job_allowed(species_type sp, job_type job) {
if (job == JOB_DELVER && crawl_state.game_is_sprint())
return CC_BANNED;
return job_allowed(sp, job);
}
string newgame_char_description(const newgame_def& ng)
{
if (_is_random_viable_choice(ng))
return "Recommended character";
else if (_is_random_choice(ng))
return "Random character";
else if (_is_random_job(ng.job))
{
const string j = (ng.job == JOB_RANDOM ? "Random " : "Recommended ");
return j + species::name(ng.species);
}
else if (_is_random_species(ng.species))
{
const string s = (ng.species == SP_RANDOM ? "Random " : "Recommended ");
return s + get_job_name(ng.job);
}
else
return species::name(ng.species) + " " + get_job_name(ng.job);
}
static string _welcome(const newgame_def& ng)
{
string text;
if (ng.species != SP_UNKNOWN)
text = species::name(ng.species);
if (ng.job != JOB_UNKNOWN)
{
if (!text.empty())
text += " ";
text += get_job_name(ng.job);
}
if (!ng.name.empty())
{
if (!text.empty())
text = " the " + text;
text = ng.name + text;
}
else if (!text.empty())
text = "unnamed " + text;
if (!text.empty())
text = ", " + text;
text = "Welcome" + text + ".";
return text;
}
void choose_tutorial_character(newgame_def& ng_choice)
{
ng_choice.species = SP_HUMAN;
ng_choice.job = JOB_FIGHTER;
ng_choice.weapon = WPN_FLAIL;
}
static void _resolve_species(newgame_def& ng, const newgame_def& ng_choice)
{
// Don't overwrite existing species.
if (ng.species != SP_UNKNOWN)
return;
if (ng_choice.species != SP_VIABLE && ng_choice.species != SP_RANDOM)
{
ng.species = ng_choice.species;
return;
}
if (!is_starting_job(ng.job)) // VIABLE, RANDOM, UNKNOWN, or disabled
{
ng.species = species::random_starting_species();
return;
}
auto candidate_species = playable_species();
if (ng_choice.species == SP_VIABLE)
{
erase_if(candidate_species, [&](const species_type sp) {
return !job_recommends_species(ng.job, sp);
});
}
else
erase_if(candidate_species, [&](const species_type sp) {
return !character_is_allowed(sp, ng.job);
});
ASSERT(candidate_species.size() > 0);
ng.species = candidate_species[random2(candidate_species.size())];
}
static void _resolve_job(newgame_def& ng, const newgame_def& ng_choice)
{
if (ng.job != JOB_UNKNOWN)
return;
if (ng_choice.job != JOB_VIABLE && ng_choice.job != JOB_RANDOM)
{
ng.job = ng_choice.job;
return;
}
if (!species::is_starting_species(ng.species)) // VIABLE, RANDOM, UNKNOWN, or disabled
{
ng.job = random_starting_job();
return;
}
auto candidate_jobs = playable_jobs();
if (ng_choice.job == JOB_VIABLE)
{
erase_if(candidate_jobs, [&](const job_type job) {
return !species::recommends_job(ng.species, job);
});
}
else
erase_if(candidate_jobs, [&](const job_type job) {
return !character_is_allowed(ng.species, job);
});
ASSERT(candidate_jobs.size() > 0);
ng.job = candidate_jobs[random2(candidate_jobs.size())];
}
static void _resolve_species_job(newgame_def& ng, const newgame_def& ng_choice)
{
// Since recommendations are no longer bidirectional, pick one of
// species or job to start. If one but not the other was specified
// as "viable", always choose that one last; otherwise use a random
// order.
const bool spfirst = ng_choice.species != SP_VIABLE
&& ng_choice.job == JOB_VIABLE;
const bool jobfirst = ng_choice.species == SP_VIABLE
&& ng_choice.job != JOB_VIABLE;
if (spfirst || !jobfirst && coinflip())
{
_resolve_species(ng, ng_choice);
_resolve_job(ng, ng_choice);
}
else
{
_resolve_job(ng, ng_choice);
_resolve_species(ng, ng_choice);
}
}
static string _highlight_pattern(const newgame_def& ng)
{
if (ng.species != SP_UNKNOWN)
return species::name(ng.species) + " ";
if (ng.job == JOB_UNKNOWN)
return "";
string ret;
for (const auto sp : playable_species())
if (species_allowed(ng.job, sp) == CC_UNRESTRICTED)
ret += species::name(sp) + " |";
if (!ret.empty())
ret.resize(ret.size() - 1);
return ret;
}
static void _prompt_choice(int choice_type, newgame_def& ng, newgame_def& ng_choice,
const newgame_def& defaults);
static void _choose_species_job(newgame_def& ng, newgame_def& ng_choice,
const newgame_def& defaults)
{
_resolve_species_job(ng, ng_choice);
while (ng_choice.species == SP_UNKNOWN || ng_choice.job == JOB_UNKNOWN)
{
// Slightly non-obvious behaviour here is due to the fact that
// both types of _prompt_choice can ask for an entirely
// random character to be rolled. They will reset relevant fields
// in ng for this purpose.
if (ng_choice.species == SP_UNKNOWN)
_prompt_choice(C_SPECIES, ng, ng_choice, defaults);
_resolve_species_job(ng, ng_choice);
if (ng_choice.job == JOB_UNKNOWN)
_prompt_choice(C_JOB, ng, ng_choice, defaults);
_resolve_species_job(ng, ng_choice);
}
if (!_job_allowed(ng.species, ng.job))
{
// Either an invalid combination was passed in through options,
// or we messed up.
end(1, false, "Incompatible species and background (%s) selected.",
newgame_char_description(ng).c_str());
}
}
// For completely random combinations (!, #, or Options.game.fully_random)
// reroll characters until the player accepts one of them or quits.
static bool _reroll_random(newgame_def& ng)
{
string specs = chop_string(species::name(ng.species), 79, false);
formatted_string prompt;
prompt.cprintf("You are a%s %s %s.",
(is_vowel(specs[0])) ? "n" : "", specs.c_str(),
get_job_name(ng.job));
auto title_hbox = make_shared<Box>(Widget::HORZ);
#ifdef USE_TILE
dolls_data doll;
fill_doll_for_newgame(doll, ng);
#ifdef USE_TILE_LOCAL
auto tile = make_shared<ui::PlayerDoll>(doll);
tile->set_margin_for_sdl(0, 10, 0, 0);
title_hbox->add_child(move(tile));
#endif
#endif
title_hbox->add_child(make_shared<Text>(prompt));
title_hbox->set_cross_alignment(Widget::CENTER);
title_hbox->set_margin_for_sdl(0, 0, 20, 0);
title_hbox->set_margin_for_crt(0, 0, 1, 0);
auto vbox = make_shared<Box>(Box::VERT);
vbox->add_child(move(title_hbox));
vbox->add_child(make_shared<Text>("Do you want to play this combination? [Y/n/q]"));
auto popup = make_shared<ui::Popup>(move(vbox));
bool done = false;
char c;
popup->on_keydown_event([&](const KeyEvent& ev) {
c = ev.key();
return done = true;
});
#ifdef USE_TILE_WEB
tiles.json_open_object();
tiles.json_write_string("prompt", prompt.to_colour_string());
tiles.send_doll(doll, false, false);
tiles.push_ui_layout("newgame-random-combo", 0);
popup->on_layout_pop([](){ tiles.pop_ui_layout(); });
#endif
ui::run_layout(move(popup), done);
if (key_is_escape(c) || toalower(c) == 'q' || crawl_state.seen_hups)
game_ended(game_exit::abort);
return toalower(c) == 'n' || c == '\t' || c == '!' || c == '#';
}
static void _choose_char(newgame_def& ng, newgame_def& choice,
newgame_def defaults)
{
const newgame_def ng_reset = ng;
if (ng.type == GAME_TYPE_TUTORIAL)
{
choose_tutorial_character(choice);
choice.allowed_jobs.clear();
choice.allowed_species.clear();
choice.allowed_weapons.clear();
}
else if (ng.type == GAME_TYPE_HINTS)
{
pick_hints(choice);
choice.allowed_jobs.clear();
choice.allowed_species.clear();
choice.allowed_weapons.clear();
}
#if defined(DGAMELAUNCH) && defined(TOURNEY)
// Apologies to non-public servers.
if (ng.type == GAME_TYPE_NORMAL)
{
if (!yesno("Trunk doesn't count for the tournament, you want "
TOURNEY ". Play trunk anyway? (Y/N)", false, 'n'))
{
game_ended(game_exit::abort);
}
}
#endif
while (true)
{
if (choice.allowed_combos.size())
{
choice.species = SP_UNKNOWN;
choice.job = JOB_UNKNOWN;
choice.weapon = WPN_UNKNOWN;
string combo =
choice.allowed_combos[random2(choice.allowed_combos.size())];
vector<string> parts = split_string(".", combo);
if (parts.size() > 0)
{
string character = trim_string(parts[0]);
if (character.length() == 4)
{
choice.species =
species::from_abbrev(character.substr(0, 2).c_str());
choice.job =
get_job_by_abbrev(
character.substr(2, 2).c_str());
}
else
{
for (const auto sp : playable_species())
{
if (starts_with(character, species::name(sp)))
{
choice.species = sp;
string temp =
character.substr(species::name(sp).length());
choice.job = str_to_job(trim_string(temp));
break;
}
}
}
if (parts.size() > 1)
{
string weapon = trim_string(parts[1]);
choice.weapon = str_to_weapon(weapon);
}
}
}
else
{
if (choice.allowed_species.size())
{
choice.species =
choice.allowed_species[
random2(choice.allowed_species.size())];
}
if (choice.allowed_jobs.size())
{
choice.job =
choice.allowed_jobs[random2(choice.allowed_jobs.size())];
}
if (choice.allowed_weapons.size())
{
choice.weapon =
choice.allowed_weapons[
random2(choice.allowed_weapons.size())];
}
}
_choose_species_job(ng, choice, defaults);
if (choice.fully_random && _reroll_random(ng))
{
ng = ng_reset;
continue;
}
if (_choose_weapon(ng, choice, defaults))
{
// We're done!
return;
}
// Else choose again, name and type stays same.
defaults = choice;
ng = ng_reset;
choice = ng_reset;
}
}
static void _add_menu_sub_item(shared_ptr<OuterMenu>& menu, int x, int y, const string& text, const string& description, char letter, int id)
{
auto tmp = make_shared<Text>();
tmp->set_text(formatted_string(text, BROWN));
tmp->set_margin_for_sdl(4,8);
tmp->set_margin_for_crt(0, 2, 0, 0);
auto btn = make_shared<MenuButton>();
btn->set_child(move(tmp));
btn->id = id;
btn->description = description;
btn->hotkey = letter;
btn->highlight_colour = STARTUP_HIGHLIGHT_CONTROL;
menu->add_button(move(btn), x, y);
}
#ifndef DGAMELAUNCH
/**
* Attempt to generate a random name for a character that doesn't collide with
* an existing save name.
*
* @return A random name, or the empty string if no good name could be
* generated after several tries.
*/
string newgame_random_name()
{
for (int i = 0; i < 100; ++i)
{
const string name = make_name();
const string filename = get_save_filename(name);
if (!save_exists(filename))
return name;
}
return "";
}
static void _choose_name(newgame_def& ng, newgame_def& choice)
{
char buf[MAX_NAME_LENGTH + 1]; // FIXME: make line_reader handle widths
buf[0] = '\0';
resumable_line_reader reader(buf, sizeof(buf));
bool done = false;
bool overwrite_prompt = false;
bool good_name = true;
bool cancel = false;
string specs = chop_string(species::name(ng.species), 79, false);
formatted_string title;
title.cprintf("You are a%s %s %s.",
(is_vowel(specs[0])) ? "n" : "", specs.c_str(),
get_job_name(ng.job));
auto title_hbox = make_shared<Box>(Widget::HORZ);
#ifdef USE_TILE
dolls_data doll;
fill_doll_for_newgame(doll, ng);
#ifdef USE_TILE_LOCAL
auto tile = make_shared<ui::PlayerDoll>(doll);
tile->set_margin_for_sdl(0, 10, 0, 0);
title_hbox->add_child(move(tile));
#endif
#endif
title_hbox->add_child(make_shared<Text>(title));
title_hbox->set_cross_alignment(Widget::CENTER);
title_hbox->set_margin_for_sdl(0, 0, 20, 0);
title_hbox->set_margin_for_crt(0, 0, 1, 0);
auto vbox = make_shared<Box>(Box::VERT);
vbox->add_child(move(title_hbox));
auto prompt_ui = make_shared<Text>();
vbox->add_child(prompt_ui);
auto sub_items = make_shared<OuterMenu>(false, 3, 1);
vbox->add_child(sub_items);
_add_menu_sub_item(sub_items, 0, 0,
"Esc - Quit", "", CK_ESCAPE, CK_ESCAPE);
_add_menu_sub_item(sub_items, 1, 0,
"* - Random name", "", '*', '*');
auto ok_switcher = make_shared<Switcher>();
ok_switcher->align_y = Widget::STRETCH;
{
auto tmp = make_shared<Text>();
tmp->set_text(formatted_string("Enter - Begin!", BROWN));
auto btn = make_shared<MenuButton>();
btn->on_activate_event([&](const ActivateEvent&) {
choice.name = buf;
trim_string(choice.name);
if (choice.name.empty())
choice.name = newgame_random_name();
if (good_name)
{
ng.name = choice.name;
ng.filename = get_save_filename(choice.name);
overwrite_prompt = save_exists(ng.filename);
if (!overwrite_prompt)
done = true;
}
return true;
});
tmp->set_margin_for_sdl(4,8);
tmp->set_margin_for_crt(0, 2, 0, 0);
btn->set_child(move(tmp));
btn->id = CK_ENTER;
btn->description = "";
btn->hotkey = CK_ENTER;
btn->highlight_colour = STARTUP_HIGHLIGHT_CONTROL;
auto err = make_shared<Text>(
formatted_string("That's a silly name!", LIGHTRED));
err->set_margin_for_sdl(0, 0, 0, 10);
auto box = make_shared<Box>(Box::HORZ);
box->set_cross_alignment(Widget::CENTER);
box->add_child(err);
ok_switcher->add_child(btn);
ok_switcher->add_child(box);
sub_items->add_button(btn, 2, 0);
}
auto popup = make_shared<ui::Popup>(move(vbox));
sub_items->on_activate_event([&](const ActivateEvent& event) {
const auto button = static_pointer_cast<MenuButton>(event.target());
const auto id = button->id;
switch (id)
{
case '*':
reader.putkey(CK_END);
reader.putkey(CONTROL('U'));
for (char ch : newgame_random_name())
reader.putkey(ch);
break;
case CK_ESCAPE:
done = cancel = true;
break;
}
return true;
});
popup->on_keydown_event([&](const KeyEvent& ev) {
auto key = ev.key();
if (!overwrite_prompt)
{
key = reader.putkey(key);
good_name = is_good_name(buf, true);
ok_switcher->current() = good_name ? 0 : 1;
}
else
{
overwrite_prompt = false;
if (key == 'Y')
return done = true;
}
return true;
});
ui::push_layout(move(popup));
while (!done && !crawl_state.seen_hups)
{
formatted_string prompt;
prompt.textcolour(CYAN);
prompt.cprintf("What is your name today? ");
prompt.textcolour(LIGHTGREY);
prompt.cprintf("%s\n", buf);
prompt.textcolour(LIGHTRED);
if (overwrite_prompt)
prompt.cprintf("You have an existing game under this name; really overwrite? [Y/n]");
prompt_ui->set_text(prompt);
ui::pump_events();
}
ui::pop_layout();
if (cancel || crawl_state.seen_hups)
game_ended(game_exit::abort);
}
#endif
static keyfun_action _keyfun_seed_input(int &ch)
{
// CK_ENTER is excluded because processing it will cause the TextEntry to
// lose focus. (TODO: maybe handle this better in TextEntry somehow?)
if (ch == CONTROL('K') || ch == CONTROL('D') || ch == CONTROL('W') ||
ch == CONTROL('U') || ch == CONTROL('A') || ch == CONTROL('E') ||
ch == CK_BKSP || ch == CK_ESCAPE ||
ch < 0 || // this should get all other special keys
isadigit(ch))
{
return KEYFUN_PROCESS;
}
return KEYFUN_IGNORE;
}
// TODO: is there a more elevant way to set this up without a full-on custom
// class?
class SeedTextEntry : public ui::TextEntry
{
public:
SeedTextEntry(ui::Text *_begin_button)
: ui::TextEntry(), begin_button(_begin_button)
{ }
bool on_event(const Event& event) override
{
#ifdef USE_TILE_LOCAL
// some ugly code duplication here, but we need to preempt the internal
// class version of pasting
if (event.type() == Event::Type::KeyDown)
{
const auto key = static_cast<const KeyEvent&>(event).key();
if (key == 'p' || key == 'P' || key == CONTROL('V'))
{
paste();
update_buttons();
return true;
}
}
#endif
bool result = ui::TextEntry::on_event(event);
if (event.type() == Event::Type::KeyDown)
update_buttons();
return result;
}
bool valid_seed()
{
return begin_button && get_text().size() > 0;
}
void update_buttons()
{
if (valid_seed())
begin_button->set_text(formatted_string("[Enter] Begin!", BROWN));
else
begin_button->set_text(formatted_string("[Enter] Begin!", DARKGRAY));
}
void set_text(const string &s) // why is it `string s` in TextEntry?
{
ui::TextEntry::set_text(s);
update_buttons();
}
#ifdef USE_TILE_LOCAL
void paste()
{
// this is set up to completely preempt
// TextEntry::LineReader::clipboard_paste
if (wm->has_clipboard())
{
string clip = wm->get_clipboard();
// try to avoid pasting in crazy garbage, by parsing as uint64
// this could be done better
uint64_t clip_seed;
int clip_found = sscanf(clip.c_str(), "%" SCNu64, &clip_seed);
if (clip_found)
set_text(make_stringf("%" PRIu64, clip_seed));
}
}
#endif
private:
ui::Text *begin_button;
};
static void _choose_seed(newgame_def& ng, newgame_def& choice,
const newgame_def& defaults)
{
UNUSED(ng, defaults);
if (Options.seed)
choice.seed = Options.seed;
else if (Options.seed_from_rc)
choice.seed = Options.seed_from_rc;
const bool show_pregen_toggle =
#ifndef DGAMELAUNCH
true;
#else
false;
#endif
bool done = false;
bool cancel = false;
auto begin_label = make_shared<ui::Text>();
begin_label->set_text(formatted_string("[Enter] Begin!", BROWN));
begin_label->set_margin_for_sdl(4,8);
begin_label->set_margin_for_crt(0, 2, 0, 0);
auto prompt_ui = make_shared<Text>();
auto box = make_shared<ui::Box>(ui::Widget::VERT);
box->set_cross_alignment(Widget::Align::STRETCH);
auto popup = make_shared<ui::Popup>(box);
const string title_text = make_stringf(
"Play a game with a custom seed for version %s.\n",
Version::Long);
box->add_child(make_shared<ui::Text>(formatted_string(title_text, CYAN)));
const string body_text = "Choose 0 for a random seed. "
"[Tab]/[Shift-Tab] to cycle input focus.\n";
box->add_child(make_shared<ui::Text>(body_text));
auto seed_hbox = make_shared<ui::Box>(ui::Box::HORZ);
box->add_child(seed_hbox);
const string prompt_text = "Seed: ";
seed_hbox->add_child(make_shared<ui::Text>(prompt_text));
auto seed_input = make_shared<SeedTextEntry>(begin_label.get());
seed_input->set_sync_id("seed");
seed_input->set_text(make_stringf("%" PRIu64, choice.seed));
seed_input->set_keyproc(_keyfun_seed_input);
#ifndef USE_TILE_LOCAL
seed_input->max_size().width = 21;
#endif
seed_hbox->add_child(seed_input);
seed_hbox->set_cross_alignment(Widget::CENTER);
auto clear_btn_label = make_shared<ui::Text>();
clear_btn_label->set_text(formatted_string("[-] Clear", BROWN));
clear_btn_label->set_margin_for_sdl(4, 4);
clear_btn_label->set_margin_for_crt(0, 1, 0, 1);
auto clear_btn = make_shared<MenuButton>();
clear_btn->set_child(move(clear_btn_label));
clear_btn->set_sync_id("btn-clear");
clear_btn->hotkey = '-';
clear_btn->set_margin_for_sdl(0,0,0,10);
clear_btn->set_margin_for_crt(0, 0, 0, 1);
clear_btn->on_activate_event([&seed_input](const ActivateEvent&) {
seed_input->set_text("");
ui::set_focused_widget(seed_input.get());
return true;
});
seed_hbox->add_child(move(clear_btn));
auto d_btn_label = make_shared<ui::Text>();
d_btn_label->set_text(formatted_string("[d] Today's daily seed", BROWN));
d_btn_label->set_margin_for_sdl(4,8);
d_btn_label->set_margin_for_crt(0, 2, 0, 0);
auto daily_seed_btn = make_shared<MenuButton>();
daily_seed_btn->set_child(move(d_btn_label));
daily_seed_btn->set_sync_id("btn-daily");
daily_seed_btn->hotkey = 'd';
daily_seed_btn->set_margin_for_sdl(0,0,0,10);
daily_seed_btn->set_margin_for_crt(0, 0, 0, 1);
daily_seed_btn->on_activate_event([&seed_input](const ActivateEvent&) {
time_t now;
time(&now);
struct tm * timeinfo = localtime(&now);
char timebuf[9];
strftime(timebuf, sizeof(timebuf), "%Y%m%d", timeinfo);
seed_input->set_text(string(timebuf));
ui::set_focused_widget(seed_input.get());
return true;
});
seed_hbox->add_child(move(daily_seed_btn));
const string footer_text =
#ifdef USE_TILE_LOCAL
"\n"
"Press [p] or [ctrl-v] to paste a seed from the clipboard\n"
"(overwriting the current value).\n"
#endif
"\n"
"The seed will determine the dungeon layout, monsters, and items\n"
"that you discover, relative to this version of crawl. Upgrading\n"
"mid-game may affect seeding. (See the manual for more details.)\n"
#ifdef SEEDING_UNRELIABLE
"Warning: your build of crawl does not support stable seeding!\n"
"Levels may differ from 'official' seeded games.\n"
#endif
;
box->add_child(make_shared<ui::Text>(footer_text));
auto pregen_check = make_shared<ui::Checkbox>();
pregen_check->set_sync_id("pregenerate");
pregen_check->set_checked(choice.pregenerate = Options.pregen_dungeon);
pregen_check->set_visible(show_pregen_toggle);
pregen_check->set_child(make_shared<ui::Text>("Fully pregenerate the dungeon"));
box->add_child(pregen_check);
auto button_hbox = make_shared<ui::Box>(ui::Box::HORZ);
button_hbox->set_main_alignment(Widget::Align::END);
box->add_child(button_hbox);
auto begin_btn = make_shared<MenuButton>();
begin_btn->set_child(begin_label);
begin_btn->set_sync_id("btn-begin");
begin_btn->hotkey = CK_ENTER;
begin_btn->on_activate_event(
[&done, &seed_input, &begin_btn](const ActivateEvent&)
{
if (!seed_input->valid_seed())
return false; // TODO: message why this failed
auto cur_focus = ui::get_focused_widget();
if (
#ifdef USE_TILE_WEB
// focus doesn't seem to be tracked right from web?
tiles.is_controlled_from_web() ||
#endif
cur_focus == begin_btn.get() || cur_focus == seed_input.get())
{
return done = true;
}
// let the other buttons activate on enter
return false;
});
button_hbox->add_child(begin_btn);
// TODO: ESC gets absorbed by active buttons, does this make sense?
popup->on_keydown_event([&](const KeyEvent& ev) {
const auto key = ev.key();
if (key == '?') // TODO: text box absorbs this still
show_help('D', "Seeded play"); // TODO: scroll to section
#ifdef USE_TILE_LOCAL
else if ((key == 'p' || key == 'P' || key == CONTROL('V')))
{
seed_input->paste();
ui::set_focused_widget(seed_input.get());
return false;
}
#endif
else if (key_is_escape(key))
return done = cancel = true;
return false;
});
#ifdef USE_TILE_WEB
tiles.json_open_object();
tiles.json_write_string("body", body_text);
tiles.json_write_string("title", title_text);
tiles.json_write_string("footer", footer_text);
tiles.json_write_bool("show_pregen_toggle", show_pregen_toggle);
tiles.push_ui_layout("seed-selection", 0);
popup->on_layout_pop([](){ tiles.pop_ui_layout(); });
#endif
ui::run_layout(move(popup), done, seed_input);
string result = seed_input->get_text();
uint64_t tmp_seed = 0;
// TODO: if the user types in a number that exceeds the max value, sscanf
// will give back the max value. Probably better to print an error?
int found = sscanf(result.c_str(), "%" SCNu64, &tmp_seed);
if (cancel || crawl_state.seen_hups || !found || result.size() == 0)
game_ended(game_exit::abort);
Options.seed = choice.seed = tmp_seed;
Options.pregen_dungeon = choice.pregenerate = pregen_check->checked();
}
// Read a choice of game into ng.
// Returns false if a game (with name ng.name) should
// be restored instead of starting a new character.
bool choose_game(newgame_def& ng, newgame_def& choice,
const newgame_def& defaults)
{
#ifdef USE_TILE_WEB
tiles.set_ui_state(UI_CRT);
// send some minimal where info, in case there is a lingering .where file
update_whereis_chargen(choice.name);
#endif
clrscr();
textcolour(LIGHTGREY);
ng.name = choice.name;
ng.type = choice.type;
ng.map = choice.map;
if (ng.type == GAME_TYPE_SPRINT
|| ng.type == GAME_TYPE_TUTORIAL)
{
_choose_gamemode_map(ng, choice, defaults);
}
else if (ng.type == GAME_TYPE_CUSTOM_SEED)
_choose_seed(ng, choice, defaults);
_choose_char(ng, choice, defaults);
// Set these again, since _mark_fully_random may reset ng.