-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
skill-menu.cc
1947 lines (1721 loc) · 51.8 KB
/
skill-menu.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 Skill menu.
**/
#include <cmath>
#include "AppHdr.h"
#include "skill-menu.h"
#include "art-enum.h"
#include "cio.h"
#include "clua.h"
#include "command.h"
#include "describe.h"
#include "english.h" // apostrophise
#include "evoke.h"
#include "god-passive.h" // passive_t::bondage_skill_boost
#include "hints.h"
#include "options.h"
#include "output.h"
#include "religion.h"
#include "state.h"
#include "stringutil.h"
#include "tilefont.h"
#ifdef USE_TILE
#include "tilepick.h"
#include "tilereg-crt.h"
#endif
#include "ui.h"
#include "unwind.h"
using namespace ui;
menu_letter2 SkillMenuEntry::m_letter;
SkillMenu skm;
#ifdef USE_TILE_LOCAL
bool SkillTextTileItem::handle_mouse(const MouseEvent& me)
{
if (me.event == MouseEvent::PRESS
&& (me.button == MouseEvent::LEFT && me.mod & TILES_MOD_SHIFT))
{
skill_type sk = skill_type(get_id());
if (is_invalid_skill(sk))
return false;
skm.select(sk, 'A');
return true;
}
else
return false;
}
#endif
#define NAME_SIZE 20
#define LEVEL_SIZE 5
#define PROGRESS_SIZE 6
#define APTITUDE_SIZE 5
SkillMenuEntry::SkillMenuEntry(coord_def coord)
{
#ifdef USE_TILE_LOCAL
m_name = new SkillTextTileItem();
#else
m_name = new TextItem();
#endif
m_level = new NoSelectTextItem();
m_progress = new EditableTextItem();
m_progress->set_editable(false);
m_aptitude = new FormattedTextItem();
m_aptitude->allow_highlight(false);
#ifdef USE_TILE_LOCAL
const int height = skm.get_line_height();
m_name->set_height(height);
if (is_set(SKMF_SKILL_ICONS))
{
m_level->set_height(height);
m_progress->set_height(height);
m_aptitude->set_height(height);
}
#endif
skm.add_item(m_name, NAME_SIZE + (is_set(SKMF_SKILL_ICONS) ? 4 : 0), coord);
m_name->set_highlight_colour(RED);
skm.add_item(m_level, LEVEL_SIZE, coord);
skm.add_item(m_progress, PROGRESS_SIZE, coord);
skm.add_item(m_aptitude, APTITUDE_SIZE, coord);
}
// Public methods
int SkillMenuEntry::get_id()
{
return m_name->get_id();
}
TextItem* SkillMenuEntry::get_name_item() const
{
return m_name;
}
skill_type SkillMenuEntry::get_skill() const
{
return m_sk;
}
static bool _show_skill(skill_type sk, skill_menu_state state)
{
switch (state)
{
case SKM_SHOW_DEFAULT:
return you.can_train[sk] || you.skill(sk, 10, false, false)
|| sk == you.transfer_from_skill || sk == you.transfer_to_skill;
case SKM_SHOW_ALL: return true;
default: return false;
}
}
bool SkillMenuEntry::is_selectable(bool keep_hotkey)
{
if (is_invalid_skill(m_sk))
return false;
if (is_set(SKMF_HELP))
return true;
if (you.species == SP_GNOLL)
return false;
if (!_show_skill(m_sk, skm.get_state(SKM_SHOW)))
return false;
if (is_set(SKMF_RESKILL_TO) && you.transfer_from_skill == m_sk)
{
if (!keep_hotkey)
++m_letter;
return false;
}
if (is_set(SKMF_RESKILL_TO) && is_useless_skill(m_sk))
return false;
if (is_set(SKMF_RESKILL_FROM) && !you.skill_points[m_sk])
{
if (!keep_hotkey)
++m_letter;
return false;
}
if (!you.can_train[m_sk] && !is_set(SKMF_RESKILL_TO)
&& !is_set(SKMF_RESKILL_FROM))
{
return false;
}
if (mastered())
{
if (is_set(SKMF_RESKILL_TO) && !keep_hotkey)
++m_letter;
if (!is_set(SKMF_RESKILL_FROM))
return false;
}
return true;
}
bool SkillMenuEntry::is_set(int flag) const
{
return skm.is_set(flag);
}
bool SkillMenuEntry::mastered() const
{
return (is_set(SKMF_EXPERIENCE) ? skm.get_raw_skill_level(m_sk)
: you.skills[m_sk]) >= MAX_SKILL_LEVEL;
}
void SkillMenuEntry::refresh(bool keep_hotkey)
{
if (m_sk == SK_TITLE)
set_title();
else if (is_invalid_skill(m_sk) || is_useless_skill(m_sk))
_clear();
else
{
set_name(keep_hotkey);
set_display();
if (is_set(SKMF_APTITUDE))
set_aptitude();
}
}
void SkillMenuEntry::set_display()
{
if (m_sk == SK_TITLE)
set_title();
if (is_invalid_skill(m_sk))
return;
switch (skm.get_state(SKM_VIEW))
{
case SKM_VIEW_TRAINING: set_training(); break;
case SKM_VIEW_COST: set_cost(); break;
case SKM_VIEW_TARGETS: set_targets(); break;
case SKM_VIEW_PROGRESS: set_progress(); break;
case SKM_VIEW_TRANSFER: set_reskill_progress(); break;
case SKM_VIEW_NEW_LEVEL: set_new_level(); break;
case SKM_VIEW_POINTS: set_points(); break;
default: die("Invalid view state.");
}
}
void SkillMenuEntry::set_name(bool keep_hotkey)
{
if (is_invalid_skill(m_sk))
return;
if (!keep_hotkey)
m_name->clear_hotkeys();
if (is_selectable(keep_hotkey))
{
if (!keep_hotkey)
{
m_name->add_hotkey(++m_letter);
m_name->add_hotkey(toupper(m_letter));
}
m_name->set_id(m_sk);
m_name->allow_highlight(true);
}
else
{
m_name->set_id(-1);
m_name->allow_highlight(false);
}
m_name->set_text(make_stringf("%s %-15s", get_prefix().c_str(),
skill_name(m_sk)));
m_name->set_fg_colour(get_colour());
#ifdef USE_TILE_LOCAL
if (is_set(SKMF_SKILL_ICONS))
{
m_name->clear_tile();
if (you.skills[m_sk] >= MAX_SKILL_LEVEL)
{
m_name->add_tile(tile_def(tileidx_skill(m_sk, TRAINING_MASTERED),
TEX_GUI));
}
else if (you.training[m_sk] == TRAINING_DISABLED)
{
m_name->add_tile(tile_def(tileidx_skill(m_sk, TRAINING_INACTIVE),
TEX_GUI));
}
else
{
m_name->add_tile(tile_def(tileidx_skill(m_sk, you.train[m_sk]),
TEX_GUI));
}
}
#endif
set_level();
}
void SkillMenuEntry::set_skill(skill_type sk)
{
m_sk = sk;
refresh(false);
}
// Private Methods
void SkillMenuEntry::_clear()
{
m_sk = SK_NONE;
m_name->set_text("");
m_level->set_text("");
m_progress->set_text("");
m_aptitude->set_text("");
m_name->set_id(-1);
m_name->clear_hotkeys();
m_name->allow_highlight(false);
#ifdef USE_TILE_LOCAL
if (is_set(SKMF_SKILL_ICONS))
m_name->clear_tile();
#endif
}
COLOURS SkillMenuEntry::get_colour() const
{
// Skills being actively trained may get further distinction.
bool use_bright_colour = you.train[m_sk] == TRAINING_ENABLED
|| you.train[m_sk] == TRAINING_FOCUSED;
if (is_set(SKMF_HELP))
return LIGHTGRAY;
else if (is_set(SKMF_RESKILL_TO) && m_sk == you.transfer_from_skill)
return BROWN;
else if (skm.get_state(SKM_VIEW) == SKM_VIEW_TRANSFER
&& (m_sk == you.transfer_from_skill
|| m_sk == you.transfer_to_skill))
{
return CYAN;
}
else if (skm.get_state(SKM_LEVEL) == SKM_LEVEL_ENHANCED
&& (you.skill(m_sk, 10, true) != you.skill(m_sk, 10, false)
// Drained a tiny but nonzero amount.
|| you.attribute[ATTR_XP_DRAIN] && you.skill_points[m_sk]))
{
if (you.skill(m_sk, 10, true) < you.skill(m_sk, 10, false))
return use_bright_colour ? LIGHTGREEN : GREEN;
else
return use_bright_colour ? LIGHTMAGENTA : MAGENTA;
}
else if (mastered())
return YELLOW;
else if (!you.training[m_sk])
return DARKGREY;
else if (skill_has_manual(m_sk))
return LIGHTBLUE;
else if (you.train[m_sk] == TRAINING_FOCUSED)
return WHITE;
else
return LIGHTGREY;
}
string SkillMenuEntry::get_prefix()
{
int letter;
const vector<int> hotkeys = m_name->get_hotkeys();
if (!hotkeys.empty())
letter = hotkeys[0];
else
letter = ' ';
const int sign = (!you.can_train[m_sk] || mastered()) ? ' ' :
(you.train[m_sk] == TRAINING_FOCUSED) ? '*' :
you.train[m_sk] ? '+'
: '-';
return make_stringf(" %c %c", letter, sign);
}
void SkillMenuEntry::set_aptitude()
{
string text = "<white>";
const bool manual = skill_has_manual(m_sk);
const int apt = species_apt(m_sk, you.species);
if (apt != 0)
text += make_stringf("%+d", apt);
else
text += make_stringf(" %d", apt);
text += "</white> ";
if (manual)
{
skm.set_flag(SKMF_MANUAL);
text += "<lightred>+4</lightred>";
}
m_aptitude->set_text(text);
}
void SkillMenuEntry::set_level()
{
int level;
const bool real = skm.get_state(SKM_LEVEL) != SKM_LEVEL_ENHANCED;
if (is_set(SKMF_EXPERIENCE))
level = skm.get_saved_skill_level(m_sk, real);
else
level = you.skill(m_sk, 10, real);
if (mastered() && !you.attribute[ATTR_XP_DRAIN])
m_level->set_text(to_string(level / 10));
else
m_level->set_text(make_stringf("%4.1f", level / 10.0));
m_level->set_fg_colour(get_colour());
}
void SkillMenuEntry::set_new_level()
{
m_progress->set_editable(false);
const bool real = skm.get_state(SKM_LEVEL) != SKM_LEVEL_ENHANCED;
int new_level = 0;
if (is_set(SKMF_EXPERIENCE) && is_selectable())
{
new_level = you.skill(m_sk, 10, real);
m_progress->set_fg_colour(CYAN);
if (you.training[m_sk])
m_progress->set_text(make_stringf("> %4.1f", new_level / 10.0));
else
m_progress->set_text(string(PROGRESS_SIZE, ' '));
return;
}
if (you.skills[m_sk] > 0 && is_set(SKMF_RESKILL_FROM)
|| m_sk == you.transfer_from_skill)
{
new_level = transfer_skill_points(m_sk, m_sk,
skill_transfer_amount(m_sk), true,
!real);
m_progress->set_fg_colour(BROWN);
}
else if (is_set(SKMF_RESKILL_TO))
{
new_level = transfer_skill_points(you.transfer_from_skill, m_sk,
you.transfer_skill_points, true,
!real);
m_progress->set_fg_colour(CYAN);
}
if (is_selectable() || m_sk == you.transfer_from_skill)
m_progress->set_text(make_stringf("> %4.1f", new_level / 10.0));
else
m_progress->set_text("");
}
void SkillMenuEntry::set_points()
{
m_progress->set_text(make_stringf("%5d", you.skill_points[m_sk]));
m_progress->set_fg_colour(LIGHTGREY);
m_progress->set_editable(false);
}
void SkillMenuEntry::set_progress()
{
if (mastered())
m_progress->set_text("");
else
{
m_progress->set_text(make_stringf(" %2d%%",
get_skill_percentage(m_sk)));
}
m_progress->set_fg_colour(CYAN);
m_progress->set_editable(false);
}
EditableTextItem *SkillMenuEntry::get_progress()
{
return m_progress;
}
void SkillMenuEntry::set_targets()
{
int target = you.get_training_target(m_sk);
if (target == 0)
{
m_progress->set_text("--");
m_progress->set_fg_colour(DARKGREY);
}
else
{
m_progress->set_text(make_stringf("%d.%d", target / 10, target % 10));
if (target_met(m_sk))
m_progress->set_fg_colour(DARKGREY); // mainly comes up in wizmode
else
m_progress->set_fg_colour(get_colour());
}
m_progress->set_editable(true, 4);
}
void SkillMenuEntry::set_reskill_progress()
{
string text;
if (m_sk == you.transfer_from_skill)
text = " * ";
else if (m_sk == you.transfer_to_skill)
{
text += make_stringf(" %2d%%",
(you.transfer_total_skill_points
- you.transfer_skill_points)
* 100 / you.transfer_total_skill_points);
}
else
text = "";
m_progress->set_text(text);
m_progress->set_fg_colour(CYAN);
}
void SkillMenuEntry::set_title()
{
m_name->allow_highlight(false);
m_name->set_text(" Skill");
m_level->set_text("Level");
m_name->set_fg_colour(BLUE);
m_level->set_fg_colour(BLUE);
m_progress->set_fg_colour(BLUE);
if (is_set(SKMF_APTITUDE))
m_aptitude->set_text("<blue>Apt </blue>");
if (is_set(SKMF_RESKILLING))
{
if (is_set(SKMF_RESKILL_FROM))
m_progress->set_text("Source");
else
m_progress->set_text("Target");
return;
}
switch (skm.get_state(SKM_VIEW))
{
case SKM_VIEW_TRAINING: m_progress->set_text("Train"); break;
case SKM_VIEW_TARGETS: m_progress->set_text("Target"); break;
case SKM_VIEW_PROGRESS: m_progress->set_text("Progr"); break;
case SKM_VIEW_TRANSFER: m_progress->set_text("Trnsf"); break;
case SKM_VIEW_POINTS: m_progress->set_text("Points");break;
case SKM_VIEW_COST: m_progress->set_text("Cost"); break;
case SKM_VIEW_NEW_LEVEL: m_progress->set_text("> New"); break;
default: die("Invalid view state.");
}
}
void SkillMenuEntry::set_training()
{
m_progress->set_editable(false);
if (!you.training[m_sk])
m_progress->set_text("");
else
m_progress->set_text(make_stringf("%2d%%", you.training[m_sk]));
m_progress->set_fg_colour(BROWN);
}
void SkillMenuEntry::set_cost()
{
m_progress->set_editable(false);
if (you.skills[m_sk] == MAX_SKILL_LEVEL)
return;
if (skill_has_manual(m_sk))
m_progress->set_fg_colour(LIGHTRED);
else
m_progress->set_fg_colour(CYAN);
auto ratio = scaled_skill_cost(m_sk);
// Don't let the displayed number go greater than 4 characters
if (ratio > 0)
m_progress->set_text(make_stringf("%4.*f", ratio < 100 ? 1 : 0, ratio));
}
SkillMenuSwitch::SkillMenuSwitch(string name, int hotkey) : m_name(name)
{
add_hotkey(hotkey);
set_highlight_colour(YELLOW);
}
void SkillMenuSwitch::add(skill_menu_state state)
{
m_states.push_back(state);
if (m_states.size() == 1)
m_state = state;
}
skill_menu_state SkillMenuSwitch::get_state()
{
return m_state;
}
static bool _any_crosstrained()
{
for (skill_type sk = SK_FIRST_SKILL; sk < NUM_SKILLS; ++sk)
{
// Assumes crosstraining is symmetric; otherwise we should
// iterate over the result of get_crosstrain_skills and
// check the levels of *those* skills
if (you.skill_points[sk]
&& !get_crosstrain_skills(sk).empty())
{
// Didn't necessarily boost us by a noticeable amount,
// but close enough.
return true;
}
}
return false;
}
string SkillMenuSwitch::get_help()
{
switch (m_state)
{
case SKM_MODE_AUTO:
return "In automatic mode, skills are trained as you use them.";
case SKM_MODE_MANUAL:
return "In manual mode, experience is spread evenly across all "
"activated skills.";
case SKM_DO_PRACTISE:
if (skm.is_set(SKMF_SIMPLE))
return hints_skills_info();
else
return "Press the letter of a skill to choose whether you want to "
"practise it. Skills marked with '<darkgrey>-</darkgrey>' "
"will not be trained.";
case SKM_DO_FOCUS:
return "Press the letter of a skill to cycle between "
"<darkgrey>disabled</darkgrey> (<darkgrey>-</darkgrey>), "
"enabled (+) and <white>focused</white> (<white>*</white>). "
"Focused skills train twice as fast relative to others.";
case SKM_LEVEL_ENHANCED:
{
string result;
if (skm.is_set(SKMF_ENHANCED))
{
vector<string> causes;
if (you.duration[DUR_HEROISM])
causes.push_back("Heroism");
if (!you.skill_boost.empty()
&& have_passive(passive_t::bondage_skill_boost))
{
causes.push_back(apostrophise(god_name(you.religion))
+ " power");
}
if (_any_crosstrained())
causes.push_back("cross-training");
result = "Skills enhanced by "
+ comma_separated_line(causes.begin(), causes.end())
+ " are in <green>green</green>.";
}
if (skm.is_set(SKMF_REDUCED))
{
vector<const char *> causes;
if (you.attribute[ATTR_XP_DRAIN])
causes.push_back("draining");
if (player_under_penance(GOD_ASHENZARI))
causes.push_back("Ashenzari's anger");
if (!result.empty())
result += "\n";
result += "Skills reduced by "
+ comma_separated_line(causes.begin(), causes.end())
+ " are in <magenta>magenta</magenta>.";
}
if (!result.empty())
return result;
}
case SKM_VIEW_TRAINING:
if (skm.is_set(SKMF_SIMPLE))
return hints_skill_training_info();
else
return "The percentage of incoming experience used"
" to train each skill is in <brown>brown</brown>.\n";
case SKM_VIEW_TARGETS:
return "The current training targets, if any.\n";
case SKM_VIEW_PROGRESS:
return "The percentage of the progress done before reaching next "
"level is in <cyan>cyan</cyan>.\n";
case SKM_VIEW_TRANSFER:
return "The progress of the knowledge transfer is displayed in "
"<cyan>cyan</cyan> in front of the skill receiving the "
"knowledge. The donating skill is marked with <cyan>*</cyan>.";
case SKM_VIEW_COST:
{
string result =
"The relative cost of raising each skill is in "
"<cyan>cyan</cyan>";
if (skm.is_set(SKMF_MANUAL))
{
result += " (or <lightred>red</lightred> if enhanced by a "
"manual)";
}
result += ".\n";
return result;
}
default: return "";
}
}
string SkillMenuSwitch::get_name(skill_menu_state state)
{
switch (state)
{
case SKM_MODE_AUTO: return "auto";
case SKM_MODE_MANUAL: return "manual";
case SKM_DO_PRACTISE: return "train";
case SKM_DO_FOCUS: return "focus";
case SKM_SHOW_DEFAULT: return "trainable";
case SKM_SHOW_ALL: return "all";
case SKM_LEVEL_ENHANCED:
return (skm.is_set(SKMF_ENHANCED)
&& skm.is_set(SKMF_REDUCED)) ? "modified" :
skm.is_set(SKMF_ENHANCED) ? "enhanced"
: "reduced";
case SKM_LEVEL_NORMAL: return "base";
case SKM_VIEW_TRAINING: return "training";
case SKM_VIEW_TARGETS: return "targets";
case SKM_VIEW_PROGRESS: return "progress";
case SKM_VIEW_TRANSFER: return "transfer";
case SKM_VIEW_POINTS: return "points";
case SKM_VIEW_NEW_LEVEL: return "new level";
case SKM_VIEW_COST: return "cost";
default: die ("Invalid switch state.");
}
}
void SkillMenuSwitch::set_state(skill_menu_state state)
{
// We only set it if it's a valid state.
for (auto candidate : m_states)
{
if (candidate == state)
{
m_state = state;
return;
}
}
}
int SkillMenuSwitch::size() const
{
if (m_states.size() <= 1)
return 0;
else
return formatted_string::parse_string(m_text).width();
}
bool SkillMenuSwitch::toggle()
{
if (m_states.size() <= 1)
return false;
auto it = find(begin(m_states), end(m_states), m_state);
ASSERT(it != m_states.end());
++it;
if (it == m_states.end())
it = m_states.begin();
m_state = *it;
update();
return true;
}
void SkillMenuSwitch::update()
{
if (m_states.size() <= 1)
{
set_text("");
return;
}
const vector<int> hotkeys = get_hotkeys();
ASSERT(hotkeys.size());
string text = make_stringf(" [<yellow>%c</yellow>] ", hotkeys[0]);
for (auto it = m_states.begin(); it != m_states.end(); ++it)
{
if (it != m_states.begin())
text += '|';
const string col = (*it == m_state) ? "white" : "darkgrey";
text += make_stringf("<%s>%s</%s>", col.c_str(), get_name(*it).c_str(),
col.c_str());
}
if (m_name.empty())
text += " ";
else
text += make_stringf(" %s ", m_name.c_str());
set_text(text);
}
#define TILES_COL 6
SkillMenu::SkillMenu() : PrecisionMenu(), m_min_coord(), m_max_coord(),
m_help_button(nullptr)
{
}
void SkillMenu::init_experience()
{
if (is_set(SKMF_EXPERIENCE) && !m_skill_backup.state_saved())
{
m_skill_backup.save();
you.auto_training = false;
reset_training();
for (int i = 0; i < NUM_SKILLS; ++i)
{
const skill_type sk = skill_type(i);
if (!is_useless_skill(sk) && !you.can_train[sk])
{
you.can_train.set(sk);
you.train[sk] = TRAINING_DISABLED;
}
}
}
}
/**
* resolve the player's experience state at the end of the skill menu, dealing
* with any experience applied to this skill menu instance (e.g. by a potion
* of experience).
*
* @param experience_change whether to actually do the experience change, or
* just do cleanup.
*/
void SkillMenu::finish_experience(bool experience_change)
{
if (is_set(SKMF_EXPERIENCE) && m_skill_backup.state_saved())
{
if (experience_change)
{
redraw_screen();
unwind_bool change_xp_for_real(crawl_state.simulating_xp_gain, false);
train_skills();
}
m_skill_backup.restore_training();
m_skill_backup = skill_state();
}
}
void SkillMenu::init(int flag, int region_height)
{
m_flags = flag;
init_flags();
init_experience();
#ifdef USE_TILE_LOCAL
const int char_height = tiles.get_crt_font()->char_height();
if (Options.tile_menu_icons)
{
line_height = min((region_height - char_height * 6) / SK_ARR_LN,
Options.tile_cell_pixels);
set_flag(SKMF_SKILL_ICONS);
}
else
line_height = char_height;
#endif
m_min_coord.x = 1;
m_min_coord.y = 1;
m_pos = m_min_coord;
m_ff = new MenuFreeform();
m_max_coord.x = MIN_COLS + 1;
#ifdef USE_TILE_LOCAL
m_max_coord.y = region_height / char_height;
if (is_set(SKMF_SKILL_ICONS))
{
m_ff->set_height(line_height);
m_max_coord.x += 2 * TILES_COL;
}
#else
m_max_coord.y = region_height + 1;
#endif
m_ff->init(m_min_coord, m_max_coord, "freeform");
attach_object(m_ff);
set_active_object(m_ff);
if (is_set(SKMF_SPECIAL))
init_title();
m_pos.x++;
const int col_split = MIN_COLS / 2
+ (is_set(SKMF_SKILL_ICONS) ? TILES_COL : 0);
for (int col = 0; col < SK_ARR_COL; ++col)
for (int ln = 0; ln < SK_ARR_LN; ++ln)
{
m_skills[ln][col] = SkillMenuEntry(coord_def(m_pos.x
+ col_split * col,
m_pos.y + ln));
}
m_pos.y += SK_ARR_LN;
#ifdef USE_TILE_LOCAL
if (is_set(SKMF_SKILL_ICONS))
m_pos.y = tiles.to_lines(m_pos.y, line_height);
else
++m_pos.y;
#endif
init_button_row();
--m_pos.x;
init_switches();
if (m_pos.y < m_max_coord.y - 1)
shift_bottom_down();
if (is_set(SKMF_SPECIAL))
set_title();
set_skills();
set_default_help();
if (is_set(SKMF_EXPERIENCE))
refresh_display();
m_highlighter = new BoxMenuHighlighter(this);
m_highlighter->init(coord_def(-1,-1), coord_def(-1,-1), "highlighter");
attach_object(m_highlighter);
m_ff->set_visible(true);
m_highlighter->set_visible(true);
refresh_button_row();
do_skill_enabled_check();
}
static keyfun_action _keyfun_target_input(int &ch)
{
if (ch == '-')
return KEYFUN_BREAK; // reset to 0
if (ch == CONTROL('K') || ch == CONTROL('D') || ch == CONTROL('W') ||
ch == CONTROL('U') || ch == CONTROL('A') || ch == CONTROL('E') ||
ch == CK_ENTER || ch == CK_BKSP || ch == CK_ESCAPE ||
ch < 0 || // this should get all other special keys
ch == '.' || isadigit(ch))
{
return KEYFUN_PROCESS;
}
return KEYFUN_IGNORE;
}
int SkillMenu::read_skill_target(skill_type sk, int keyn)
{
SkillMenuEntry *entry = find_entry(sk);
ASSERT(entry);
EditableTextItem *progress = entry->get_progress();
ASSERT(progress);
const int old_target = you.get_training_target(sk);
const string prefill = old_target <= 0 ? "0"
: make_stringf("%d.%d", old_target / 10, old_target % 10);
progress->set_editable(true, 5);
progress->set_highlight_colour(RED);
// for webtiles dialog input
progress->set_prompt(make_stringf("Enter a skill target for %s: ",
skill_name(sk)));
progress->set_tag("skill_target");
edit_result r = progress->edit(&prefill, _keyfun_target_input);
const char *result_buf = r.text.c_str();
int input;
if (r.reader_result == '-')
input = 0;
else if (r.reader_result || result_buf[0] == '\0')
{
// editing canceled
cancel_set_target();
return -1;
}
else
input = round(atof(result_buf) * 10.0); // TODO: parse fixed point?
you.set_training_target(sk, input);
cancel_set_target();
refresh_display();
return input;
}
void SkillMenu::clear()
{
PrecisionMenu::clear(); // deletes m_ff, which deletes its entries
m_switches.clear();
m_ff = nullptr;
m_help_button = nullptr;
m_middle_button = nullptr;
m_clear_targets_button = nullptr;
m_skill_backup = skill_state();
m_flags = 0;
}
//Public methods
void SkillMenu::clear_flag(int flag)
{
m_flags &= ~flag;
}
bool SkillMenu::is_set(int flag) const
{
return m_flags & flag;
}
void SkillMenu::set_flag(int flag)
{
m_flags |= flag;
}
void SkillMenu::toggle_flag(int flag)
{
m_flags ^= flag;
}
void SkillMenu::add_item(TextItem* item, const int size, coord_def &coord)
{
if (coord.x + size > m_max_coord.x)
{