/
item-use.cc
4255 lines (3732 loc) · 123 KB
/
item-use.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 for making use of inventory items.
**/
#include "AppHdr.h"
#include "item-use.h"
#include "ability.h"
#include "acquire.h"
#include "act-iter.h"
#include "areas.h"
#include "artefact.h"
#include "art-enum.h"
#include "chardump.h"
#include "cloud.h"
#include "colour.h"
#include "coordit.h"
#include "corpse.h"
#include "database.h"
#include "delay.h"
#include "describe.h"
#include "directn.h"
#include "english.h"
#include "env.h"
#include "evoke.h"
#include "fight.h"
#include "god-conduct.h"
#include "god-item.h"
#include "god-passive.h"
#include "hints.h"
#include "invent.h"
#include "item-prop.h"
#include "item-status-flag-type.h"
#include "items.h"
#include "known-items.h"
#include "level-state-type.h"
#include "libutil.h"
#include "macro.h"
#include "makeitem.h"
#include "message.h"
#include "misc.h"
#include "mon-behv.h"
#include "mutation.h"
#include "nearby-danger.h"
#include "orb.h"
#include "output.h"
#include "player-equip.h"
#include "player-stats.h"
#include "potion.h"
#include "prompt.h"
#include "religion.h"
#include "shout.h"
#include "skills.h"
#include "sound.h"
#include "spl-book.h"
#include "spl-clouds.h"
#include "spl-goditem.h"
#include "spl-selfench.h"
#include "spl-summoning.h"
#include "spl-transloc.h"
#include "spl-wpnench.h"
#include "state.h"
#include "stringutil.h"
#include "tag-version.h"
#include "target.h"
#include "terrain.h"
#include "throw.h"
#include "tiles-build-specific.h"
#include "transform.h"
#include "uncancel.h"
#include "unwind.h"
#include "view.h"
#include "xom.h"
static bool _equip_oper(operation_types oper);
// The menu class for using items from either inv or floor.
// Derivative of InvMenu
class UseItemMenu : public InvMenu
{
void reset(operation_types oper, const char* prompt_override=nullptr);
bool init_modes();
bool populate_list(bool check_only=false);
void populate_menu();
bool process_key(int key) override;
void update_sections();
void clear() override;
bool examine_index(int i) override;
bool cycle_mode(bool forward) override;
void save_hover();
void restore_hover(bool preserve_pos);
string get_keyhelp(bool scrollable) const override;
bool skip_process_command(int keyin) override
{
// override superclass behavior for everything except id menu
if (keyin == '!' && item_type_filter != OSEL_UNIDENT)
return false;
return InvMenu::skip_process_command(keyin);
}
command_type get_command(int keyin) override
{
if (keyin == '?' && item_type_filter != OSEL_UNIDENT)
return CMD_MENU_EXAMINE;
return InvMenu::get_command(keyin);
}
public:
UseItemMenu(operation_types oper, int selector, const char* prompt);
bool display_all;
bool is_inventory;
int item_type_filter;
operation_types oper;
int saved_inv_item;
int saved_hover;
int last_inv_pos;
// XX these probably shouldn't be const...
vector<const item_def*> item_inv;
vector<const item_def*> item_floor;
void toggle_display_all();
void toggle_inv_or_floor();
void set_hovered(int hovered, bool force=false) override;
bool cycle_headers(bool=true) override;
bool empty_check() const;
bool show_unarmed() const
{
return oper == OPER_WIELD || oper == OPER_EQUIP;
}
bool show_floor() const
{
return oper != OPER_EVOKE;
}
bool allow_full_inv() const
{
switch (oper)
{
case OPER_WIELD:
case OPER_WEAR:
case OPER_PUTON:
return true;
default: // disallow for everything else, incl OPER_EQUIP, OPER_ANY
return false;
}
}
private:
MenuEntry *inv_header;
MenuEntry *floor_header;
vector<operation_types> available_modes;
};
static string _default_use_title(operation_types oper)
{
switch (oper)
{
case OPER_EQUIP:
return Options.equip_unequip
? "Equip or unequip which item?"
: "Equip which item?";
case OPER_WIELD:
return Options.equip_unequip
? "Wield or unwield which item (- for none)?"
: "Wield which item (- for none)?";
case OPER_WEAR:
return Options.equip_unequip
? "Wear or take off which item?"
: "Wear which item?";
case OPER_PUTON:
return Options.equip_unequip
? "Put on or remove which piece of jewellery?"
: "Put on which piece of jewellery?";
case OPER_QUAFF:
if (you.has_mutation(MUT_LONG_TONGUE))
return "Slurp which item?";
return "Drink which item?";
case OPER_READ:
return "Read which item?";
case OPER_EVOKE:
return "Evoke which item?";
case OPER_TAKEOFF:
return "Take off which piece of armour?";
case OPER_REMOVE:
return "Remove which piece of jewellery?";
case OPER_UNEQUIP:
return "Unequip which item?";
default:
return "buggy";
}
}
// XX code dup with invent.cc, _operation_verb
static string _oper_name(operation_types oper)
{
switch (oper)
{
case OPER_EQUIP: return "equip";
case OPER_WIELD: return "wield";
case OPER_WEAR: return "wear";
case OPER_PUTON: return "put on";
case OPER_QUAFF: return "quaff";
case OPER_READ: return "read";
case OPER_EVOKE: return "evoke";
case OPER_TAKEOFF: return "take off";
case OPER_REMOVE: return "remove";
case OPER_UNEQUIP: return "unequip";
default:
return "buggy";
}
}
static int _default_osel(operation_types oper)
{
switch (oper)
{
case OPER_WIELD:
return OSEL_WIELD; // XX is this different from OBJ_WEAPONS any more?
case OPER_WEAR:
return OBJ_ARMOUR;
case OPER_PUTON:
return OBJ_JEWELLERY;
case OPER_QUAFF:
return OBJ_POTIONS;
case OPER_READ:
return OBJ_SCROLLS;
case OPER_EVOKE:
return OSEL_EVOKABLE;
case OPER_EQUIP:
return OSEL_EQUIPABLE;
case OPER_TAKEOFF:
return OSEL_WORN_ARMOUR;
case OPER_REMOVE:
return OSEL_WORN_JEWELLERY;
case OPER_UNEQUIP:
return OSEL_WORN_EQUIPABLE;
default:
return OSEL_ANY; // buggy?
}
}
static vector<operation_types> _oper_to_mode(operation_types o)
{
static const vector<operation_types> wearables
= {OPER_EQUIP, OPER_WIELD, OPER_WEAR, OPER_PUTON};
static const vector<operation_types> removables
= {OPER_UNEQUIP, OPER_TAKEOFF, OPER_REMOVE};
static const vector<operation_types> usables
= {OPER_READ, OPER_QUAFF, OPER_EVOKE};
// return a copy
vector<operation_types> result;
if (find(wearables.begin(), wearables.end(), o) != wearables.end())
result = wearables;
else if (find(usables.begin(), usables.end(), o) != usables.end())
result = usables;
else if (find(removables.begin(), removables.end(), o) != removables.end())
result = removables;
return result;
}
bool UseItemMenu::init_modes()
{
available_modes = _oper_to_mode(oper);
if (available_modes.empty())
return false;
{
unwind_var<operation_types> cur_oper(oper);
unwind_var<int> cur_filter(item_type_filter);
erase_if(available_modes, [cur_oper, this](operation_types o) {
oper = o;
item_type_filter = _default_osel(oper);
return !populate_list(true);
});
}
if (available_modes.size() == 0)
return false;
// somewhat hardcoded. In some circumstances, we can end up with a menu that
// has two identical modes; in which case, it's best to show only the mode
// that the menu caller requested.
// XX this could be more general if it actually checked menu items?
if (available_modes.size() == 2
&& (available_modes[0] == OPER_EQUIP
|| available_modes[0] == OPER_UNEQUIP && !you.weapon()))
{
erase_if(available_modes, [this](operation_types o) {
return oper != o;
});
}
if (available_modes.size() == 0)
return false;
// the originally requested oper did not survive (e.g. because there are no
// items that match it, or it was removed above); fall back on whatever is
// first. Except for the hardcoded case above, this should be equip/unequip.
if (_equip_oper(oper)
&& find(available_modes.begin(), available_modes.end(), oper)
== available_modes.end())
{
oper = available_modes[0];
}
return true;
}
bool UseItemMenu::cycle_mode(bool forward)
{
if (available_modes.size() <= 1)
return false;
auto it = find(available_modes.begin(), available_modes.end(), oper);
if (it == available_modes.end())
return false; // or assert?
// ugly iterator clock math
if (forward)
it = it + 1 == available_modes.end() ? available_modes.begin() : it + 1;
else
it = it == available_modes.begin() ? available_modes.end() - 1 : it - 1;
oper = *it;
save_hover();
item_type_filter = _default_osel(oper);
// always reset display all on mode change
display_all = false;
clear();
populate_list();
populate_menu();
update_sections();
set_title(_default_use_title(oper));
restore_hover(true);
return true;
}
class TempUselessnessHighlighter : public MenuHighlighter
{
public:
int entry_colour(const MenuEntry *entry) const override
{
return entry->colour != MENU_ITEM_STOCK_COLOUR ? entry->colour
: entry->highlight_colour(true);
}
};
void UseItemMenu::reset(operation_types _oper, const char* prompt_override)
{
oper = _oper;
display_all = false;
clear();
// init_modes may change oper
init_modes();
if (prompt_override)
set_title(prompt_override);
else
set_title(_default_use_title(oper));
// see `item_is_selected` for more on what can be used for item_type.
if (item_type_filter == OSEL_ANY)
item_type_filter = _default_osel(oper);
populate_list();
populate_menu();
// for wield in particular:
// start hover on wielded item, if there is one, otherwise on -
if (oper == OPER_WIELD && item_inv.size() > 0 && you.weapon())
set_hovered(1);
update_title();
update_more();
}
UseItemMenu::UseItemMenu(operation_types _oper, int item_type=OSEL_ANY,
const char* prompt=nullptr)
: InvMenu(MF_SINGLESELECT | MF_ARROWS_SELECT
| MF_INIT_HOVER | MF_ALLOW_FORMATTING),
display_all(false), is_inventory(true),
item_type_filter(item_type), oper(_oper), saved_inv_item(NON_ITEM),
saved_hover(-1), last_inv_pos(-1), inv_header(nullptr),
floor_header(nullptr)
{
set_tag("use_item");
set_flags(get_flags() & ~MF_USE_TWO_COLUMNS);
set_highlighter(new TempUselessnessHighlighter()); // pointer managed by Menu
menu_action = ACT_EXECUTE;
reset(oper, prompt);
}
bool UseItemMenu::populate_list(bool check_only)
{
if (check_only && show_unarmed())
return true; // any char can go unarmed
// Load inv items first
for (const auto &item : you.inv)
{
if (item.defined()
&& (display_all && allow_full_inv()
|| item_is_selected(item, item_type_filter)))
{
if (check_only)
return true;
item_inv.push_back(&item);
}
}
if (show_floor())
{
// Load floor items...
vector<const item_def*> floor = const_item_list_on_square(
you.visible_igrd(you.pos()));
for (const auto *it : floor)
{
// ...only stuff that can go into your inventory though
if (!it->defined() || item_is_stationary(*it) || item_is_orb(*it)
|| item_is_spellbook(*it) || it->base_type == OBJ_GOLD
|| it->base_type == OBJ_RUNES)
{
continue;
}
// even with display_all, only show matching floor items.
if (!item_is_selected(*it, item_type_filter))
continue;
if (check_only)
return true;
item_floor.push_back(it);
}
}
return !check_only && (item_inv.size() > 0 || item_floor.size() > 0);
}
bool UseItemMenu::empty_check() const
{
// (if choosing weapons, then bare hands are always a possibility)
if (!show_unarmed()
&& !any_items_of_type(item_type_filter, -1, show_floor()))
{
mprf(MSGCH_PROMPT, "%s",
no_selectables_message(item_type_filter).c_str());
return true;
}
return false;
}
static void _note_tele_cancel(MenuEntry* entry)
{
auto ie = dynamic_cast<InvEntry *>(entry);
if (ie && ie->item
&& ie->item->base_type == OBJ_SCROLLS
&& ie->item->sub_type == SCR_TELEPORTATION
&& you.duration[DUR_TELEPORT])
{
ie->text += " (cancels current teleport)";
}
}
void UseItemMenu::populate_menu()
{
if (item_inv.empty())
is_inventory = false;
else if (item_floor.empty())
is_inventory = true;
// Entry for unarmed. Hotkey works for either subsection, though selecting
// it (currently) enables the inv section.
if (show_unarmed())
{
string hands_string = you.unarmed_attack_name("Unarmed");
if (!you.weapon())
hands_string += " (current attack)";
MenuEntry *hands = new MenuEntry(hands_string, MEL_ITEM);
if (!you.weapon())
hands->colour = LIGHTGREEN;
hands->add_hotkey('-');
hands->on_select = [this](const MenuEntry&)
{
lastch = '-';
return false;
};
add_entry(hands);
}
if (!item_inv.empty())
{
// Only clarify that these are inventory items if there are also floor
// items.
if (!item_floor.empty())
{
inv_header = new MenuEntry("Inventory Items", MEL_TITLE);
inv_header->colour = LIGHTCYAN;
add_entry(inv_header);
}
load_items(item_inv,
[&](MenuEntry* entry) -> MenuEntry*
{
// hacky: remove the class hotkey for cases where it
// is counterintuitive/useless
if (item_type_filter != OSEL_UNIDENT)
entry->hotkeys.pop_back();
if (item_type_filter == OBJ_SCROLLS)
_note_tele_cancel(entry);
return entry;
});
}
last_inv_pos = items.size() - 1;
if (!item_floor.empty())
{
#ifndef USE_TILE
// vertical padding for console
if (!item_inv.empty())
add_entry(new MenuEntry("", MEL_TITLE));
#endif
// Load floor items to menu. Always add a subtitle, even if there are
// no inv items.
floor_header = new MenuEntry("Floor Items", MEL_TITLE);
floor_header->colour = LIGHTCYAN;
add_entry(floor_header);
load_items(item_floor,
[&](MenuEntry* entry) -> MenuEntry*
{
// hacky: remove the class hotkey
entry->hotkeys.pop_back();
if (item_type_filter == OBJ_SCROLLS)
_note_tele_cancel(entry);
return entry;
});
}
update_sections();
if (last_hovered >= 0 && !item_floor.empty() && !item_inv.empty())
{
if (is_inventory && last_hovered > last_inv_pos)
set_hovered(show_unarmed() ? 1 : 0);
else if (!is_inventory && last_hovered <= last_inv_pos)
set_hovered(last_inv_pos + 1);
}
}
static bool _enable_equip_check(operation_types o, MenuEntry *entry)
{
auto ie = dynamic_cast<InvEntry *>(entry);
if (o != OPER_EQUIP && o != OPER_WIELD && o != OPER_PUTON && o != OPER_WEAR)
return true;
return !ie || !ie->item || !item_is_equipped(*(ie->item))
|| (item_is_equipped(*(ie->item)) && Options.equip_unequip);
}
static bool _disable_item(const MenuEntry &)
{
// XX would be nice to show an informative message for this case
return true;
}
static item_def *_entry_to_item(MenuEntry *me)
{
if (!me)
return nullptr;
auto ie = dynamic_cast<InvEntry *>(me);
if (!ie)
return nullptr;
auto tgt = const_cast<item_def*>(ie->item);
return tgt;
}
void UseItemMenu::save_hover()
{
// when switch modes, we want to keep hover on the currently selected item
// even if it moves around, or failing that, in a similar position in the
// menu. This code accomplishes that.
saved_hover = last_hovered;
if (last_hovered < 0)
{
saved_inv_item = NON_ITEM;
return;
}
auto tgt = _entry_to_item(items[last_hovered]);
if (!tgt || !in_inventory(*tgt))
{
saved_inv_item = NON_ITEM;
return;
}
saved_inv_item = tgt->link;
}
void UseItemMenu::restore_hover(bool preserve_pos)
{
// if there is a saved hovered item, look for that first
if (saved_inv_item != NON_ITEM)
for (int i = 0; i < static_cast<int>(items.size()); i++)
if (auto tgt = _entry_to_item(items[i]))
if (tgt->link == saved_inv_item)
{
set_hovered(i);
saved_inv_item = NON_ITEM;
saved_hover = -1;
return;
}
// nothing found
saved_inv_item = NON_ITEM;
const bool use_hover = is_set(MF_ARROWS_SELECT);
if (!preserve_pos || !use_hover)
set_hovered(use_hover ? 0 : -1);
else
{
// if preserve_pos is set, try to keep an existing saved hover position
set_hovered(saved_hover);
}
saved_hover = -1;
// fixup hover in case headings have moved
if (last_hovered >= 0 && items[last_hovered]->level != MEL_ITEM)
cycle_hover();
}
void UseItemMenu::update_sections()
{
// disable unarmed on equip menus if player is unarmed
if (show_unarmed() && !you.weapon())
items[0]->on_select = _disable_item;
int i = show_unarmed() ? 1 : 0;
for (; i <= last_inv_pos; i++)
if (items[i]->level == MEL_ITEM)
{
items[i]->set_enabled(is_inventory);
if (is_inventory && !_enable_equip_check(oper, items[i]))
items[i]->on_select = _disable_item;
}
for (; i < static_cast<int>(items.size()); i++)
if (items[i]->level == MEL_ITEM)
items[i]->set_enabled(!is_inventory);
const string cycle_hint = make_stringf("<lightgray> (%s to select)</lightgray>",
menu_keyhelp_cmd(CMD_MENU_CYCLE_HEADERS).c_str());
// a `,` will trigger quick activation, rather than cycle headers
const bool easy_floor = Options.easy_floor_use && item_floor.size() == 1
&& (is_inventory || !inv_header);
if (inv_header)
{
inv_header->text = "Inventory Items";
if (!is_inventory)
inv_header->text += cycle_hint;
}
if (floor_header)
{
floor_header->text = "Floor Items";
if (easy_floor)
{
floor_header->text += make_stringf(" (%s to %s)",
menu_keyhelp_cmd(CMD_MENU_CYCLE_HEADERS).c_str(),
_oper_name(oper).c_str());
}
else if (is_inventory)
floor_header->text += cycle_hint;
}
update_menu(true);
}
void UseItemMenu::clear()
{
item_inv.clear();
item_floor.clear();
inv_header = nullptr;
floor_header = nullptr;
last_inv_pos = -1;
Menu::clear();
}
void UseItemMenu::toggle_display_all()
{
// don't allow identifying already id'd items, etc
if (!allow_full_inv())
return;
save_hover();
clear();
display_all = !display_all;
populate_list();
populate_menu();
update_sections();
update_more();
restore_hover(true);
}
void UseItemMenu::toggle_inv_or_floor()
{
if (item_inv.empty() || item_floor.empty())
return;
is_inventory = !is_inventory;
update_sections();
}
// only two sections, and we want to skip inv menu subtitles, so it's simplest
// just to write custom code here
bool UseItemMenu::cycle_headers(bool)
{
if (item_inv.empty() || item_floor.empty())
return false;
if (!is_set(MF_ARROWS_SELECT))
toggle_inv_or_floor();
else if (is_inventory)
set_hovered(last_inv_pos + 1);
else
set_hovered(0);
// XX this skips `unarmed`, should it?
cycle_hover(); // hover is guaranteed to be a header, get to a selectable item
#ifdef USE_TILE_WEB
// cycle_headers doesn't currently have a client-side
// implementation, so force-send the server-side scroll
webtiles_update_scroll_pos(true);
#endif
return true;
}
void UseItemMenu::set_hovered(int hovered, bool force)
{
// need to be a little bit careful about recursion potential here:
// update_menu calls set_hovered to sanitize low level UI state.
const bool skip_toggle = hovered == last_hovered;
InvMenu::set_hovered(hovered, force);
// keep inv vs floor in sync
if (!skip_toggle && last_hovered >= 0
&& !item_floor.empty() && !item_inv.empty())
{
if (is_inventory && last_hovered > last_inv_pos
|| !is_inventory && last_hovered <= last_inv_pos)
{
toggle_inv_or_floor();
}
}
}
bool UseItemMenu::examine_index(int i)
{
if (show_unarmed() && i == 0)
return true; // no description implemented
else if (is_inventory)
return InvMenu::examine_index(i);
else // floor item
{
auto desc_tgt = _entry_to_item(items[i]);
ASSERT(desc_tgt);
return describe_item(*desc_tgt, nullptr, false);
}
}
string UseItemMenu::get_keyhelp(bool) const
{
string r;
// XX the logic here is getting convoluted, if only these were widgets...
const string desc_key = is_set(MF_ARROWS_SELECT)
? "[<w>?</w>] describe selected" : "";
string full;
string eu_modes;
string modes;
if (allow_full_inv())
{
full = "[<w>*</w>] ";
full += (display_all ? "show appropriate" : "show all");
}
// OPER_ANY menus are identify, enchant, brand; they disable most of these
// key shortcuts
if (oper != OPER_ANY)
{
if (_equip_oper(oper))
{
eu_modes = "[<w>tab</w>] ";
eu_modes += (generalize_oper(oper) == OPER_EQUIP)
? "<w>equip</w>|unequip " : "equip|<w>unequip</w> ";
}
if (available_modes.size() > 1)
{
vector<string> mode_names;
for (const auto &o : available_modes)
{
string n = _oper_name(o);
if (o == oper)
n = "<w>" + n + "</w>";
mode_names.push_back(n);
}
modes = menu_keyhelp_cmd(CMD_MENU_CYCLE_MODE) + " "
+ join_strings(mode_names.begin(), mode_names.end(), "|")
+ " ";
}
}
if (eu_modes.size() && desc_key.size() && modes.size() && full.size())
{
// too much stuff to fit in one row
r = pad_more_with(full, desc_key) + "\n";
r += pad_more_with(modes, eu_modes);
return r;
}
else
{
// if both of these are shown, always keep them in a stable position
if (eu_modes.size() && desc_key.size())
r = pad_more_with("", desc_key);
// annoying: I cannot get more height changes to work correctly. So as
// a workaround, this will produce a two line more, potentially
// with one blank line (except on menus where mode never changes).
if (oper != OPER_ANY)
r += "\n";
r += modes + full;
if (eu_modes.size() && desc_key.size())
return pad_more_with(r, eu_modes); // desc_key on prev line
else // at most one of these is shown
return pad_more_with(r, desc_key.size() ? desc_key : eu_modes);
}
}
bool UseItemMenu::process_key(int key)
{
// TODO: should check inscriptions here
if (isadigit(key)
|| key == '-' && show_unarmed())
{
lastch = key;
return false;
}
if (key == '*')
{
toggle_display_all();
return true;
}
else if (key == '\\')
{
check_item_knowledge();
return true;
}
else if (key == ','
&& Options.easy_floor_use && item_floor.size() == 1
&& (is_inventory || item_inv.empty()))
{
// TODO: should this go with CMD_MENU_CYCLE_HEADERS instead of `,`?
lastch = ','; // XX don't use keycode for this
return false;
}
else if (key == CK_TAB && _equip_oper(oper))
{
item_type_filter = OSEL_ANY;
save_hover();
switch (oper)
{
case OPER_EQUIP:
case OPER_WIELD: reset(OPER_UNEQUIP); break;
case OPER_WEAR: reset(OPER_TAKEOFF); break;
case OPER_PUTON: reset(OPER_REMOVE); break;
case OPER_UNEQUIP: reset(OPER_EQUIP); break;
case OPER_REMOVE: reset(OPER_PUTON); break;
case OPER_TAKEOFF: reset(OPER_WEAR); break;
default: break;
}
restore_hover(false);
return true;
}
return Menu::process_key(key);
}
static operation_types _item_to_oper(item_def *target)
{
if (!target)
return OPER_WIELD; // unwield
switch (target->base_type)
{
case OBJ_WANDS:
case OBJ_MISCELLANY: return OPER_EVOKE;
case OBJ_POTIONS: return OPER_QUAFF;
case OBJ_SCROLLS: return OPER_READ;
case OBJ_ARMOUR: return OPER_WEAR;
case OBJ_WEAPONS:
case OBJ_STAVES: return OPER_WIELD;
case OBJ_JEWELLERY: return OPER_PUTON;
default: return OPER_NONE;
}
}
static operation_types _item_to_removal(item_def *target)
{
if (!target)
return OPER_NONE;
switch (target->base_type)
{
case OBJ_ARMOUR: return OPER_TAKEOFF;
case OBJ_WEAPONS:
case OBJ_STAVES: return OPER_WIELD;
case OBJ_JEWELLERY: return OPER_REMOVE;
default: return OPER_NONE;
}
}
static bool _equip_oper(operation_types oper)
{
const auto gen = generalize_oper(oper);
return gen == OPER_EQUIP || gen == OPER_UNEQUIP;
}
static bool _can_generically_use_armour(bool wear=true)
{
if (you.berserk())
{
canned_msg(MSG_TOO_BERSERK);
return false;
}
if (you.has_mutation(MUT_NO_ARMOUR))
{
if (wear)
mprf(MSGCH_PROMPT, "You can't wear anything.");
else
{
mprf(MSGCH_PROMPT, "You can't remove your %s, sorry.",
species::skin_name(you.species).c_str());
}
return false;
}
if (!form_can_wear())
{
mprf(MSGCH_PROMPT, "You can't %s anything in your present form.",
wear ? "wear" : "remove");
return false;
}
return true;
}
static bool _can_generically_use(operation_types oper)
{
// check preconditions
// XX should this be part of the menu?
string err;
switch (oper)
{
case OPER_QUAFF:
err = cannot_drink_item_reason();
break;
case OPER_READ:
err = cannot_read_item_reason();
break;
case OPER_EVOKE:
err = cannot_evoke_item_reason();
break;
case OPER_WEAR:
if (!_can_generically_use_armour())
return false;
break;
case OPER_WIELD:
if (!can_wield(nullptr, true, false))
return false;
break;
case OPER_REMOVE:
// TODO: maybe add a worn jewellery check here?
// intentional fallthrough
case OPER_PUTON:
if (you.berserk())
{
canned_msg(MSG_TOO_BERSERK);
return false;
}
// can't differentiate between these two at this point
if (!you_can_wear(EQ_RINGS, true) && !you_can_wear(EQ_AMULET, true))
{
mprf(MSGCH_PROMPT, "You can't %s jewellery in your present form.",
oper == OPER_PUTON ? "wear" : "remove");
return false;
}
break;
case OPER_TAKEOFF:
if (!_can_generically_use_armour(false))
return false;