forked from cataclysmbn/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_inventory.cpp
More file actions
2260 lines (1879 loc) · 82.9 KB
/
game_inventory.cpp
File metadata and controls
2260 lines (1879 loc) · 82.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "game_inventory.h"
#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstddef>
#include <functional>
#include <map>
#include <memory>
#include <numeric>
#include <optional>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#include "avatar.h"
#include "avatar_functions.h"
#include "bionics.h"
#include "calendar.h"
#include "cata_utility.h"
#include "crafting.h"
#include "character.h"
#include "character_functions.h"
#include "character_martial_arts.h"
#include "color.h"
#include "cursesdef.h"
#include "damage.h"
#include "debug.h"
#include "enums.h"
#include "flag.h"
#include "examine_item_menu.h"
#include "game.h"
#include "input.h"
#include "inventory.h"
#include "inventory_ui.h"
#include "item.h"
#include "itype.h"
#include "iuse.h"
#include "iuse_actor.h"
#include "item_category.h"
#include "material.h"
#include "map.h"
#include "npc.h"
#include "options.h"
#include "output.h"
#include "player.h"
#include "player_activity.h"
#include "point.h"
#include "recipe.h"
#include "recipe_dictionary.h"
#include "requirements.h"
#include "ret_val.h"
#include "skill.h"
#include "stomach.h"
#include "string_formatter.h"
#include "string_id.h"
#include "string_utils.h"
#include "translations.h"
#include "type_id.h"
#include "ui_manager.h"
#include "units.h"
#include "units_utility.h"
#include "value_ptr.h"
#include "salvage.h"
static const activity_id ACT_EAT_MENU( "ACT_EAT_MENU" );
static const activity_id ACT_CONSUME_FOOD_MENU( "ACT_CONSUME_FOOD_MENU" );
static const activity_id ACT_CONSUME_DRINK_MENU( "ACT_CONSUME_DRINK_MENU" );
static const activity_id ACT_CONSUME_MEDS_MENU( "ACT_CONSUME_MEDS_MENU" );
static const fault_id fault_bionic_nonsterile( "fault_bionic_nonsterile" );
static const skill_id skill_computer( "computer" );
static const skill_id skill_electronics( "electronics" );
static const skill_id skill_firstaid( "firstaid" );
static const quality_id qual_ANESTHESIA( "ANESTHESIA" );
static const bionic_id bio_painkiller( "bio_painkiller" );
static const bionic_id bio_taste_blocker( "bio_taste_blocker" );
static const trait_id trait_DEBUG_BIONICS( "DEBUG_BIONICS" );
static const trait_id trait_NOPAIN( "NOPAIN" );
static const trait_id trait_SAPROPHAGE( "SAPROPHAGE" );
static const trait_id trait_SAPROVORE( "SAPROVORE" );
static const trait_id trait_INFRESIST( "INFRESIST" );
static const std::string flag_LIQUIDCONT( "LIQUIDCONT" );
static const std::string iuse_TOGGLE_UPS_CHARGING( "TOGGLE_UPS_CHARGING" );
static const flag_id flag_BIONIC_NPC_USABLE( "BIONIC_NPC_USABLE" );
using item_filter = std::function<bool ( const item & )>;
class inventory_filter_preset : public inventory_selector_preset
{
public:
inventory_filter_preset( const item_filter &filter );
bool is_shown( const item *location ) const override;
private:
item_filter filter;
};
namespace
{
std::string good_bad_none( int value )
{
if( value > 0 ) {
return string_format( "<good>+%d</good>", value );
} else if( value < 0 ) {
return string_format( "<bad>%d</bad>", value );
}
return std::string();
}
int anesthetic_requirement( int mult )
{
const requirement_data req_anesth = *requirement_id( "anesthetic" ) * mult;
if( !req_anesth.get_tools().empty() && !req_anesth.get_tools().front().empty() ) {
return req_anesth.get_tools().front().front().count;
}
return 0;
}
} // namespace
inventory_filter_preset::inventory_filter_preset( const item_filter &filter )
: filter( filter )
{}
bool inventory_filter_preset::is_shown( const item *location ) const
{
return filter( *location );
}
static item *inv_internal( player &u, const inventory_selector_preset &preset,
const std::string &title, int radius,
const std::string &none_message,
const std::string &hint = std::string(),
bool include_fake_bionics = false )
{
inventory_pick_selector inv_s( u, preset );
inv_s.set_title( title );
inv_s.set_hint( hint );
inv_s.set_display_stats( false );
std::pair<size_t, size_t> init_pair;
bool init_selection = false;
std::string init_filter;
bool has_init_filter = false;
const std::vector<activity_id> consuming {
ACT_EAT_MENU,
ACT_CONSUME_FOOD_MENU,
ACT_CONSUME_DRINK_MENU,
ACT_CONSUME_MEDS_MENU };
if( u.has_activity( consuming ) && u.activity->values.size() >= 2 ) {
init_pair.first = u.activity->values[0];
init_pair.second = u.activity->values[1];
init_selection = true;
}
if( u.has_activity( consuming ) && !u.activity->str_values.empty() ) {
init_filter = u.activity->str_values[0];
has_init_filter = true;
}
do {
u.inv_restack( );
inv_s.clear_items();
inv_s.add_character_items( u );
inv_s.add_nearby_items( radius );
if( include_fake_bionics ) {
inv_s.add_bionics_items( u );
}
if( has_init_filter ) {
inv_s.set_filter( init_filter );
has_init_filter = false;
}
// Set position after filter to keep cursor at the right position
if( init_selection ) {
inv_s.select_position( init_pair );
init_selection = false;
}
if( inv_s.empty() ) {
const std::string msg = none_message.empty()
? _( "You don't have the necessary item at hand." )
: none_message;
popup( msg, PF_GET_KEY );
return nullptr;
}
item *location = inv_s.execute();
if( inv_s.keep_open ) {
inv_s.keep_open = false;
continue;
}
if( u.has_activity( consuming ) ) {
u.activity->values.clear();
init_pair = inv_s.get_selection_position();
u.activity->values.push_back( init_pair.first );
u.activity->values.push_back( init_pair.second );
u.activity->str_values.clear();
u.activity->str_values.emplace_back( inv_s.get_filter() );
}
return location;
} while( true );
}
void game_menus::inv::common( avatar &you )
{
inventory_pick_selector inv_s( you );
inv_s.set_title( _( "Inventory" ) );
inv_s.set_hint( string_format(
_( "Item hotkeys assigned: <color_light_gray>%d</color>/<color_light_gray>%d</color>" ),
you.allocated_invlets().count(), inv_chars.size() ) );
bool started_action = false;
do {
you.inv_restack( );
inv_s.clear_items();
inv_s.add_character_items( you );
item *location = inv_s.execute();
if( location == nullptr ) {
if( inv_s.keep_open ) {
inv_s.keep_open = false;
continue;
} else {
break;
}
}
const auto func_pos_x = []() {
return 0;
};
const auto func_width = []() {
return 50;
};
started_action = examine_item_menu::run( *location, func_pos_x, func_width,
examine_item_menu::menu_pos_t::right );
} while( !started_action );
}
item *game_menus::inv::titled_filter_menu( const item_filter &filter, avatar &you,
const std::string &title, const std::string &none_message )
{
return inv_internal( you, inventory_filter_preset( filter ),
title, -1, none_message );
}
item *game_menus::inv::titled_menu( avatar &you, const std::string &title,
const std::string &none_message )
{
const std::string msg = none_message.empty() ? _( "Your inventory is empty." ) : none_message;
return inv_internal( you, inventory_selector_preset(), title, -1, msg );
}
class armor_inventory_preset: public inventory_selector_preset
{
public:
armor_inventory_preset( player &pl, const std::string &color_in ) :
p( pl ), color( color_in ) {
append_cell( [ this ]( const item * loc ) {
return get_number_string( loc->get_avg_encumber( p ) );
}, _( "AVG ENCUMBRANCE" ) );
append_cell( [ this ]( const item * loc ) {
return loc->get_storage() > 0_ml ? string_format( "<%s>%s</color>", color,
format_volume( loc->get_storage() ) ) : std::string();
}, _( "STORAGE" ) );
append_cell( [ this ]( const item * loc ) {
return string_format( "<%s>%d%%</color>", color, loc->get_avg_coverage() );
}, _( "AVG COVERAGE" ) );
append_cell( [ this ]( const item * loc ) {
return get_number_string( loc->get_warmth() );
}, _( "WARMTH" ) );
append_cell( [ this ]( const item * loc ) {
return get_number_string( loc->bash_resist() );
}, _( "BASH" ) );
append_cell( [ this ]( const item * loc ) {
return get_number_string( loc->cut_resist() );
}, _( "CUT" ) );
append_cell( [ this ]( const item * loc ) {
return get_number_string( loc->bullet_resist() );
}, _( "BULLET" ) );
append_cell( [ this ]( const item * loc ) {
return get_number_string( loc->acid_resist() );
}, _( "ACID" ) );
append_cell( [ this ]( const item * loc ) {
return get_number_string( loc->fire_resist() );
}, _( "FIRE" ) );
append_cell( [ this ]( const item * loc ) {
return get_number_string( loc->get_env_resist() );
}, _( "ENV" ) );
}
protected:
player &p;
private:
std::string get_number_string( int number ) const {
return number ? string_format( "<%s>%d</color>", color, number ) : std::string();
}
const std::string color;
};
class wear_inventory_preset: public armor_inventory_preset
{
public:
wear_inventory_preset( player &p, const std::string &color ) :
armor_inventory_preset( p, color )
{}
bool is_shown( const item *loc ) const override {
return loc->is_armor() && !p.is_worn( *loc );
}
std::string get_denial( const item *loc ) const override {
const auto ret = p.can_wear( *loc );
if( !ret.success() ) {
return trim_punctuation_marks( ret.str() );
}
return std::string();
}
};
item *game_menus::inv::wear( player &p )
{
return inv_internal( p, wear_inventory_preset( p, "color_yellow" ), _( "Wear item" ), 1,
_( "You have nothing to wear." ) );
}
class take_off_inventory_preset: public armor_inventory_preset
{
public:
take_off_inventory_preset( player &p, const std::string &color ) :
armor_inventory_preset( p, color )
{}
bool is_shown( const item *loc ) const override {
return loc->is_armor() && p.is_worn( *loc );
}
std::string get_denial( const item *loc ) const override {
const ret_val<bool> ret = p.can_takeoff( *loc );
if( !ret.success() ) {
return trim_punctuation_marks( ret.str() );
}
return std::string();
}
};
item *game_menus::inv::take_off( avatar &you )
{
return inv_internal( you, take_off_inventory_preset( you, "color_red" ), _( "Take off item" ), 1,
_( "You aren't wearing anything." ) );
}
item *game::inv_map_splice( const item_filter &filter, const std::string &title, int radius,
const std::string &none_message )
{
return inv_internal( u, inventory_filter_preset( filter ),
title, radius, none_message );
}
item *game_menus::inv::container_for( avatar &you, const item &liquid, int radius )
{
const auto filter = [ &liquid ]( const item & location ) {
// Reject containers that already contain a different liquid (different set_vars)
if( location.is_container() && !location.is_container_empty() ) {
const item &cont_liq = location.get_contained();
// Compare set_vars – if they don't match, skip this container. used for DNA comparison
if( cont_liq.get_var( "specimen_sample" ) != liquid.get_var( "specimen_sample" ) ) {
return false;
}
}
if( location.where() == item_location_type::character ) {
Character *character = g->critter_at<Character>( location.position() );
if( character == nullptr ) {
debugmsg( "Invalid location supplied to the liquid filter: no character found." );
return false;
}
return location.get_remaining_capacity_for_liquid( liquid, *character ) > 0;
}
const bool allow_buckets = location.where() == item_location_type::map;
return location.get_remaining_capacity_for_liquid( liquid, allow_buckets ) > 0;
};
return inv_internal( you, inventory_filter_preset( filter ),
string_format( _( "Container for %s" ), liquid.display_name( liquid.charges ) ), radius,
string_format( _( "You don't have a suitable container for carrying %s." ),
liquid.tname() ) );
}
class pickup_inventory_preset : public inventory_selector_preset
{
public:
pickup_inventory_preset( const player &p ) : p( p ) {}
std::string get_denial( const item *loc ) const override {
if( !p.has_item( *loc ) ) {
if( loc->made_of( LIQUID ) ) {
return _( "Can't pick up spilt liquids" );
} else if( !p.can_pick_volume( *loc ) && p.is_armed() ) {
return _( "Too big to pick up" );
} else if( !p.can_pick_weight( *loc, !get_option<bool>( "DANGEROUS_PICKUPS" ) ) ) {
return _( "Too heavy to pick up" );
}
}
return std::string();
}
private:
const player &p;
};
class disassemble_inventory_preset : public pickup_inventory_preset
{
public:
disassemble_inventory_preset( const player &p, const inventory &inv ) :
pickup_inventory_preset( p ), p( p ), inv( inv ) {
check_components = true;
append_cell( [ this ]( const item * loc ) {
const auto &req = get_recipe( loc ).disassembly_requirements();
if( req.is_empty() ) {
return std::string();
}
const item *i = loc;
const auto components = i->get_uncraft_components();
return enumerate_as_string( components.begin(), components.end(),
[]( const decltype( components )::value_type & comps ) {
return comps.to_string();
} );
}, _( "YIELD" ) );
append_cell( [ this ]( const item * loc ) {
return to_string_clipped( time_duration::from_turns( get_recipe( loc ).time / 100 ) );
}, _( "TIME" ) );
}
bool is_shown( const item *loc ) const override {
return get_recipe( loc );
}
std::string get_denial( const item *loc ) const override {
const ret_val<bool> ret = crafting::can_disassemble( p, *loc, inv );
if( !ret.success() ) {
return ret.str();
}
return pickup_inventory_preset::get_denial( loc );
}
protected:
const recipe &get_recipe( const item *loc ) const {
return recipe_dictionary::get_uncraft( loc->typeId() );
}
private:
const player &p;
const inventory &inv;
};
item *game_menus::inv::disassemble( player &p )
{
return inv_internal( p, disassemble_inventory_preset( p, p.crafting_inventory() ),
_( "Disassemble item" ), 1,
_( "You don't have any items you could disassemble." ) );
}
class comestible_inventory_preset : public inventory_selector_preset
{
public:
comestible_inventory_preset( const player &p ) : p( p ) {
append_cell( [ &p, this ]( const item * loc ) {
const nutrients nutr = p.compute_effective_nutrients( get_consumable_item( loc ) );
return good_bad_none( nutr.kcal );
}, _( "CALORIES" ) );
append_cell( [ this ]( const item * loc ) {
return good_bad_none( get_edible_comestible( loc ).quench );
}, _( "QUENCH" ) );
append_cell( [ &p, this ]( const item * loc ) {
const int consume_fun = p.fun_for( get_consumable_item( loc ) ).first;
if( consume_fun < 0 && p.has_active_bionic( bio_taste_blocker ) &&
p.get_power_level() > units::from_kilojoule( -consume_fun ) ) {
return string_format( "<color_light_gray>[%d]</color>", consume_fun );
} else {
return good_bad_none( consume_fun );
}
}, _( "JOY" ) );
append_cell( [ this ]( const item * loc ) {
const time_duration spoils = get_edible_comestible( loc ).spoils;
if( spoils > 0_turns ) {
return to_string_clipped( spoils );
}
//~ Used for permafood shelf life in the Eat menu
return std::string( _( "indefinite" ) );
}, _( "SHELF LIFE" ) );
append_cell( [ this ]( const item * loc ) {
const item &it = get_consumable_item( loc );
int converted_volume_scale = 0;
const int charges = std::max( it.charges, 1 );
const double converted_volume = round_up( convert_volume( it.volume().value() / charges,
&converted_volume_scale ), 2 );
//~ Eat menu Volume: <num><unit>
return string_format( _( "%.2f%s" ), converted_volume, volume_units_abbr() );
}, _( "VOLUME" ) );
append_cell( [this]( const item * loc ) {
if( g->u.can_estimate_rot() ) {
const islot_comestible item = get_edible_comestible( loc );
if( item.spoils > 0_turns ) {
return get_freshness( loc );
}
return std::string( "---" );
}
return std::string();
}, _( "FRESHNESS" ) );
append_cell( [ this ]( const item * loc ) {
if( g->u.can_estimate_rot() ) {
const islot_comestible item = get_edible_comestible( loc );
if( item.spoils > 0_turns ) {
if( !get_consumable_item( loc ).rotten() ) {
return get_time_left_rounded( loc );
}
}
return std::string( "---" );
}
return std::string();
}, _( "SPOILS IN" ) );
append_cell( [ this, &p ]( const item * loc ) {
std::string cbm_name;
switch( p.get_cbm_rechargeable_with( get_consumable_item( loc ) ) ) {
case rechargeable_cbm::none:
break;
case rechargeable_cbm::reactor:
cbm_name = _( "Reactor" );
break;
case rechargeable_cbm::furnace:
cbm_name = _( "Furnace" );
break;
case rechargeable_cbm::other:
std::vector<bionic_id> bids = p.get_bionic_fueled_with( get_consumable_item( loc ) );
if( !bids.empty() ) {
bionic_id bid = p.get_most_efficient_bionic( bids );
cbm_name = bid->name.translated();
}
break;
}
if( !cbm_name.empty() ) {
return string_format( "<color_cyan>%s</color>", cbm_name );
}
return std::string();
}, _( "CBM" ) );
append_cell( [ this, &p ]( const item * loc ) {
return good_bad_none( p.get_acquirable_energy( get_consumable_item( loc ) ) );
}, _( "ENERGY (kJ)" ) );
}
bool is_shown( const item *loc ) const override {
// If an item was inserted into a non-container, we can't eat it.
// For example, we couldn't eat an item mod made of meat
return p.can_consume( *loc ) &&
( loc->where() != item_location_type::container || loc->parent_item()->is_container() );
}
std::string get_denial( const item *loc ) const override {
const item &med = !( *loc ).is_container_empty() && ( *loc ).get_contained().is_medication() &&
( *loc ).get_contained().type->has_use() ? ( *loc ).get_contained() : *loc;
if( loc->made_of( LIQUID ) && !g->m.has_flag( flag_LIQUIDCONT, loc->position() ) ) {
return _( "Can't drink spilt liquids" );
}
if( med.is_medication() && !p.can_use_heal_item( med ) ) {
return _( "Your biology is not compatible with that item." );
}
const auto &it = get_consumable_item( loc );
const auto res = p.can_eat( it );
const auto cbm = p.get_cbm_rechargeable_with( it );
if( !res.success() && cbm == rechargeable_cbm::none ) {
return res.str();
} else if( cbm == rechargeable_cbm::other && ( p.get_fuel_capacity( it.typeId() ) <= 0 ) ) {
return string_format( _( "No space to store more %s" ), it.tname() );
}
return inventory_selector_preset::get_denial( loc );
}
bool sort_compare( const inventory_entry &lhs, const inventory_entry &rhs ) const override {
time_duration time_a = get_time_left( lhs.any_item() );
time_duration time_b = get_time_left( rhs.any_item() );
int order_a = get_order( lhs.any_item(), time_a );
int order_b = get_order( rhs.any_item(), time_b );
return order_a < order_b
|| ( order_a == order_b && time_a < time_b )
|| ( order_a == order_b && time_a == time_b &&
inventory_selector_preset::sort_compare( lhs, rhs ) );
}
protected:
int get_order( const item *loc, const time_duration &time ) const {
if( time > 0_turns && !( loc->type->container && loc->type->container->preserves ) ) {
return 0;
} else if( get_consumable_item( loc ).rotten() ) {
if( p.has_trait( trait_SAPROPHAGE ) || p.has_trait( trait_SAPROVORE ) ) {
return 1;
} else {
return 4;
}
} else if( time == 0_turns ) {
return 3;
} else {
return 2;
}
}
// WARNING: this can return consumables which are not necessarily possessing
// the comestible type. please dereference responsibly.
const item &get_consumable_item( const item *loc ) const {
return p.get_consumable_from( const_cast<item &>( *loc ) );
}
const islot_comestible &get_edible_comestible( const item *loc ) const {
return get_edible_comestible( get_consumable_item( loc ) );
}
const islot_comestible &get_edible_comestible( const item &it ) const {
if( it.is_comestible() && p.can_eat( it ).success() ) {
// Ok since can_eat() returns false if is_craft() is true
return *it.type->comestible;
}
static const islot_comestible dummy {};
return dummy;
}
time_duration get_time_left( const item *loc ) const {
time_duration time_left = 0_turns;
const time_duration shelf_life = get_edible_comestible( loc ).spoils;
if( shelf_life > 0_turns ) {
const item &it = get_consumable_item( loc );
const double relative_rot = it.get_relative_rot();
time_left = shelf_life - shelf_life * relative_rot;
// Correct for an estimate that exceeds shelf life -- this happens especially with
// fresh items.
if( time_left > shelf_life ) {
time_left = shelf_life;
}
}
return time_left;
}
std::string get_time_left_rounded( const item *loc ) const {
const item &it = get_consumable_item( loc );
if( it.is_going_bad() ) {
return _( "soon!" );
}
time_duration time_left = get_time_left( loc );
return to_string_approx( time_left );
}
std::string get_freshness( const item *loc ) {
const item &it = get_consumable_item( loc );
const double rot_progress = it.get_relative_rot();
if( it.is_fresh() ) {
return _( "fresh" );
} else if( rot_progress < 0.3 ) {
return _( "quite fresh" );
} else if( rot_progress < 0.5 ) {
return _( "near midlife" );
} else if( rot_progress < 0.7 ) {
return _( "past midlife" );
} else if( rot_progress < 0.9 ) {
return _( "getting older" );
} else if( !it.rotten() ) {
return _( "old" );
} else {
return _( "rotten" );
}
}
private:
const player &p;
};
static std::string get_consume_needs_hint( player &p )
{
auto hint = std::string();
auto desc = p.get_hunger_description();
hint.append( string_format( "%s %s", _( "Food :" ), colorize( desc.first, desc.second ) ) );
hint.append( string_format( " %s ", LINE_XOXO_S ) );
desc = p.get_thirst_description();
hint.append( string_format( "%s %s", _( "Drink:" ), colorize( desc.first, desc.second ) ) );
hint.append( string_format( " %s ", LINE_XOXO_S ) );
desc = p.get_pain_description();
hint.append( string_format( "%s %s", _( "Pain :" ), colorize( desc.first, desc.second ) ) );
hint.append( string_format( " %s ", LINE_XOXO_S ) );
desc = p.get_fatigue_description();
hint.append( string_format( "%s %s", _( "Rest :" ), colorize( desc.first, desc.second ) ) );
return hint;
}
item *game_menus::inv::consume( player &p )
{
if( !g->u.has_activity( ACT_EAT_MENU ) ) {
g->u.assign_activity( ACT_EAT_MENU );
}
return inv_internal( p, comestible_inventory_preset( p ),
_( "Consume item" ), 1,
_( "You have nothing to consume." ),
get_consume_needs_hint( p ) );
}
class comestible_filtered_inventory_preset : public comestible_inventory_preset
{
public:
comestible_filtered_inventory_preset( const player &p, bool( *predicate )( const item &it ) ) :
comestible_inventory_preset( p ), predicate( predicate ) {}
bool is_shown( const item *loc ) const override {
return comestible_inventory_preset::is_shown( loc ) &&
predicate( get_consumable_item( loc ) );
}
private:
bool( *predicate )( const item &it );
};
item *game_menus::inv::consume_food( player &p )
{
if( !g->u.has_activity( ACT_CONSUME_FOOD_MENU ) ) {
g->u.assign_activity( ACT_CONSUME_FOOD_MENU );
}
return inv_internal( p, comestible_filtered_inventory_preset( p, []( const item & it ) {
return ( it.is_comestible() && it.get_comestible()->comesttype == "FOOD" ) ||
it.has_flag( flag_USE_EAT_VERB );
} ),
_( "Consume food" ), 1,
_( "You have no food to consume." ),
get_consume_needs_hint( p ) );
}
item *game_menus::inv::consume_drink( player &p )
{
if( !g->u.has_activity( ACT_CONSUME_DRINK_MENU ) ) {
g->u.assign_activity( ACT_CONSUME_DRINK_MENU );
}
return inv_internal( p, comestible_filtered_inventory_preset( p, []( const item & it ) {
return it.is_comestible() && it.get_comestible()->comesttype == "DRINK" &&
!it.has_flag( flag_USE_EAT_VERB );
} ),
_( "Consume drink" ), 1,
_( "You have no drink to consume." ),
get_consume_needs_hint( p ) );
}
item *game_menus::inv::consume_meds( player &p )
{
if( !g->u.has_activity( ACT_CONSUME_MEDS_MENU ) ) {
g->u.assign_activity( ACT_CONSUME_MEDS_MENU );
}
return inv_internal( p, comestible_filtered_inventory_preset( p, []( const item & it ) {
return it.is_medication();
} ),
_( "Consume medication" ), 1,
_( "You have no medication to consume." ),
get_consume_needs_hint( p ) );
}
class activatable_inventory_preset : public pickup_inventory_preset
{
public:
activatable_inventory_preset( const player &p ) : pickup_inventory_preset( p ), p( p ) {
if( get_option<bool>( "INV_USE_ACTION_NAMES" ) ) {
append_cell( [ this ]( const item * loc ) {
const item &it = !( *loc ).is_container_empty() && ( *loc ).get_contained().is_medication() &&
( *loc ).get_contained().type->has_use() ? ( *loc ).get_contained() : *loc;
return string_format( "<color_light_green>%s</color>", get_action_name( it ) );
}, _( "ACTION" ) );
}
}
bool is_shown( const item *loc ) const override {
if( !( *loc ).is_container_empty() && ( *loc ).get_contained().is_medication() &&
( *loc ).get_contained().type->has_use() ) {
return true;
}
return loc->type->has_use();
}
std::string get_denial( const item *loc ) const override {
const item &it = !( *loc ).is_container_empty() && ( *loc ).get_contained().is_medication() &&
( *loc ).get_contained().type->has_use() ? ( *loc ).get_contained() : *loc;
const auto &uses = it.type->use_methods;
if( uses.size() == 1 ) {
const auto ret = uses.begin()->second.can_call( p, it, false, p.pos() );
if( !ret.success() ) {
return trim_punctuation_marks( ret.str() );
}
}
if( it.is_medication() && !p.can_use_heal_item( it ) && !it.is_craft() ) {
return _( "Your biology is not compatible with that item." );
}
if( !p.has_enough_charges( it, false ) && !uses.contains( iuse_TOGGLE_UPS_CHARGING ) ) {
return string_format(
vgettext( "Needs at least %d charge",
"Needs at least %d charges", loc->ammo_required() ),
loc->ammo_required() );
}
if( !it.has_flag( flag_ALLOWS_REMOTE_USE ) ) {
return pickup_inventory_preset::get_denial( loc );
}
return std::string();
}
protected:
std::string get_action_name( const item &it ) const {
const auto &uses = it.type->use_methods;
if( uses.size() == 1 ) {
return uses.begin()->second.get_name();
} else if( uses.size() > 1 ) {
return _( "…" );
}
return std::string();
}
private:
const player &p;
};
item *game_menus::inv::use( avatar &you )
{
return inv_internal( you, activatable_inventory_preset( you ),
_( "Use item" ), 1,
_( "You don't have any items you can use." ),
std::string(), true );
}
class gunmod_inventory_preset : public inventory_selector_preset
{
public:
gunmod_inventory_preset( const player &p, const item &gunmod ) : p( p ), gunmod( gunmod ) {
append_cell( [ this ]( const item * loc ) {
const auto odds = get_odds( loc );
if( odds.first >= 100 ) {
return string_format( "<color_light_green>%s</color>", _( "always" ) );
}
return string_format( "<color_light_green>%d%%</color>", odds.first );
}, _( "SUCCESS CHANCE" ) );
append_cell( [ this ]( const item * loc ) {
return good_bad_none( get_odds( loc ).second );
}, _( "DAMAGE RISK" ) );
}
bool is_shown( const item *loc ) const override {
return loc->is_gun() && !loc->is_gunmod();
}
std::string get_denial( const item *loc ) const override {
const auto ret = loc->is_gunmod_compatible( gunmod );
if( !ret.success() ) {
return ret.str();
}
if( !p.meets_requirements( gunmod, &*loc ) ) {
return string_format( _( "requires at least %s" ),
p.enumerate_unmet_requirements( gunmod, &*loc ) );
}
if( get_odds( loc ).first <= 0 ) {
return _( "is too difficult for you to modify" );
}
return std::string();
}
bool sort_compare( const inventory_entry &lhs, const inventory_entry &rhs ) const override {
const auto a = get_odds( lhs.any_item() );
const auto b = get_odds( rhs.any_item() );
if( a.first > b.first || ( a.first == b.first && a.second < b.second ) ) {
return true;
}
return inventory_selector_preset::sort_compare( lhs, rhs );
}
protected:
/** @return Odds for successful installation (pair.first) and gunmod damage (pair.second) */
std::pair<int, int> get_odds( const item *gun ) const {
return avatar_funcs::gunmod_installation_odds( *p.as_avatar(), *gun, gunmod );
}
private:
const player &p;
const item &gunmod;
};
item *game_menus::inv::gun_to_modify( player &p, const item &gunmod )
{
return inv_internal( p, gunmod_inventory_preset( p, gunmod ),
_( "Select gun to modify" ), -1,
_( "You don't have any guns to modify." ) );
}
class read_inventory_preset final: public inventory_selector_preset
{
public:
read_inventory_preset( const player &p ) : p( p ) {
const std::string unknown = _( "<color_dark_gray>?</color>" );
append_cell( [ this, &p ]( const item * loc ) -> std::string {
if( loc->type->can_use( "MA_MANUAL" ) ) {
return _( "martial arts" );
}
const islot_book &book = get_book( loc );
if( !book.skill ) {
return std::string();
}
const SkillLevel &skill = p.get_skill_level_object( book.skill );
if( skill.level() < book.req ) {
//~ %1$s: book skill name, %3$d: book required skill level, %3$d: book skill level, %4$d: player skill level
return string_format( pgettext( "skill", "%1$s from %2$d to %3$d (%4$d)" ), book.skill->name(),
book.req, book.level,
skill.level() );
}
//~ %1$s: book skill name, %2$d: book skill level, %3$d: player skill level
const auto result = string_format( pgettext( "skill", "%1$s to %2$d (%3$d)" ),
book.skill->name(), book.level, skill.level() );