-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathconsumption.cpp
1901 lines (1702 loc) · 76.8 KB
/
consumption.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <functional>
#include <memory>
#include <string>
#include <tuple>
#include "addiction.h"
#include "avatar.h"
#include "bionics.h"
#include "calendar.h"
#include "cata_utility.h"
#include "character.h"
#include "color.h"
#include "contents_change_handler.h"
#include "craft_command.h"
#include "debug.h"
#include "effect.h"
#include "effect_on_condition.h"
#include "enums.h"
#include "event.h"
#include "event_bus.h"
#include "flag.h"
#include "flat_set.h"
#include "game.h"
#include "item.h"
#include "item_category.h"
#include "itype.h"
#include "iuse.h"
#include "iuse_actor.h"
#include "line.h"
#include "make_static.h"
#include "map.h"
#include "material.h"
#include "messages.h"
#include "monster.h"
#include "morale_types.h"
#include "mutation.h"
#include "npc.h"
#include "options.h"
#include "pickup.h"
#include "recipe.h"
#include "recipe_dictionary.h"
#include "requirements.h"
#include "rng.h"
#include "stomach.h"
#include "string_formatter.h"
#include "text_snippets.h"
#include "translations.h"
#include "units.h"
#include "value_ptr.h"
#include "visitable.h"
#include "vitamin.h"
static const std::string comesttype_DRINK( "DRINK" );
static const std::string comesttype_FOOD( "FOOD" );
static const bionic_id bio_faulty_grossfood( "bio_faulty_grossfood" );
static const bionic_id bio_syringe( "bio_syringe" );
static const bionic_id bio_taste_blocker( "bio_taste_blocker" );
static const efftype_id effect_bloodworms( "bloodworms" );
static const efftype_id effect_brainworms( "brainworms" );
static const efftype_id effect_common_cold( "common_cold" );
static const efftype_id effect_flu( "flu" );
static const efftype_id effect_foodpoison( "foodpoison" );
static const efftype_id effect_fungus( "fungus" );
static const efftype_id effect_hallu( "hallu" );
static const efftype_id effect_hunger_engorged( "hunger_engorged" );
static const efftype_id effect_hunger_famished( "hunger_famished" );
static const efftype_id effect_hunger_full( "hunger_full" );
static const efftype_id effect_hunger_near_starving( "hunger_near_starving" );
static const efftype_id effect_hunger_starving( "hunger_starving" );
static const efftype_id effect_nausea( "nausea" );
static const efftype_id effect_paincysts( "paincysts" );
static const efftype_id effect_poison( "poison" );
static const efftype_id effect_tapeworm( "tapeworm" );
static const efftype_id effect_took_thorazine( "took_thorazine" );
static const efftype_id effect_visuals( "visuals" );
static const flag_id json_flag_ALLERGEN_BREAD( "ALLERGEN_BREAD" );
static const flag_id json_flag_ALLERGEN_CHEESE( "ALLERGEN_CHEESE" );
static const flag_id json_flag_ALLERGEN_DRIED_VEGETABLE( "ALLERGEN_DRIED_VEGETABLE" );
static const flag_id json_flag_ALLERGEN_EGG( "ALLERGEN_EGG" );
static const flag_id json_flag_ALLERGEN_FRUIT( "ALLERGEN_FRUIT" );
static const flag_id json_flag_ALLERGEN_MEAT( "ALLERGEN_MEAT" );
static const flag_id json_flag_ALLERGEN_MILK( "ALLERGEN_MILK" );
static const flag_id json_flag_ALLERGEN_NUT( "ALLERGEN_NUT" );
static const flag_id json_flag_ALLERGEN_VEGGY( "ALLERGEN_VEGGY" );
static const flag_id json_flag_ALLERGEN_WHEAT( "ALLERGEN_WHEAT" );
static const flag_id json_flag_ANIMAL_PRODUCT( "ANIMAL_PRODUCT" );
static const item_category_id item_category_chems( "chems" );
static const itype_id itype_apparatus( "apparatus" );
static const itype_id itype_dab_pen_on( "dab_pen_on" );
static const itype_id itype_syringe( "syringe" );
static const json_character_flag json_flag_BLOODFEEDER( "BLOODFEEDER" );
static const json_character_flag json_flag_CANNIBAL( "CANNIBAL" );
static const json_character_flag json_flag_HEMOVORE( "HEMOVORE" );
static const json_character_flag json_flag_IMMUNE_SPOIL( "IMMUNE_SPOIL" );
static const json_character_flag json_flag_NUMB( "NUMB" );
static const json_character_flag json_flag_PARAIMMUNE( "PARAIMMUNE" );
static const json_character_flag json_flag_PRED1( "PRED1" );
static const json_character_flag json_flag_PRED2( "PRED2" );
static const json_character_flag json_flag_PRED3( "PRED3" );
static const json_character_flag json_flag_PRED4( "PRED4" );
static const json_character_flag json_flag_PSYCHOPATH( "PSYCHOPATH" );
static const json_character_flag json_flag_SAPIOVORE( "SAPIOVORE" );
static const json_character_flag json_flag_SPIRITUAL( "SPIRITUAL" );
static const json_character_flag json_flag_STRICT_HUMANITARIAN( "STRICT_HUMANITARIAN" );
static const material_id material_all( "all" );
static const mtype_id mon_player_blob( "mon_player_blob" );
static const mutation_category_id mutation_category_URSINE( "URSINE" );
static const skill_id skill_cooking( "cooking" );
static const skill_id skill_survival( "survival" );
static const trait_id trait_ACIDBLOOD( "ACIDBLOOD" );
static const trait_id trait_AMORPHOUS( "AMORPHOUS" );
static const trait_id trait_ANTIFRUIT( "ANTIFRUIT" );
static const trait_id trait_ANTIJUNK( "ANTIJUNK" );
static const trait_id trait_ANTIWHEAT( "ANTIWHEAT" );
static const trait_id trait_CARNIVORE( "CARNIVORE" );
static const trait_id trait_EATDEAD( "EATDEAD" );
static const trait_id trait_EATHEALTH( "EATHEALTH" );
static const trait_id trait_GOURMAND( "GOURMAND" );
static const trait_id trait_HERBIVORE( "HERBIVORE" );
static const trait_id trait_HIBERNATE( "HIBERNATE" );
static const trait_id trait_LACTOSE( "LACTOSE" );
static const trait_id trait_MEATARIAN( "MEATARIAN" );
static const trait_id trait_M_DEPENDENT( "M_DEPENDENT" );
static const trait_id trait_M_IMMUNE( "M_IMMUNE" );
static const trait_id trait_PICKYEATER( "PICKYEATER" );
static const trait_id trait_PROBOSCIS( "PROBOSCIS" );
static const trait_id trait_PROJUNK( "PROJUNK" );
static const trait_id trait_PROJUNK2( "PROJUNK2" );
static const trait_id trait_RUMINANT( "RUMINANT" );
static const trait_id trait_SAPROPHAGE( "SAPROPHAGE" );
static const trait_id trait_SAPROVORE( "SAPROVORE" );
static const trait_id trait_SCHIZOPHRENIC( "SCHIZOPHRENIC" );
static const trait_id trait_SLIMESPAWNER( "SLIMESPAWNER" );
static const trait_id trait_SPIRITUAL( "SPIRITUAL" );
static const trait_id trait_STIMBOOST( "STIMBOOST" );
static const trait_id trait_TABLEMANNERS( "TABLEMANNERS" );
static const trait_id trait_THRESH_BIRD( "THRESH_BIRD" );
static const trait_id trait_THRESH_CATTLE( "THRESH_CATTLE" );
static const trait_id trait_THRESH_FELINE( "THRESH_FELINE" );
static const trait_id trait_THRESH_LUPINE( "THRESH_LUPINE" );
static const trait_id trait_THRESH_MOUSE( "THRESH_MOUSE" );
static const trait_id trait_THRESH_PLANT( "THRESH_PLANT" );
static const trait_id trait_THRESH_RABBIT( "THRESH_RABBIT" );
static const trait_id trait_THRESH_RAT( "THRESH_RAT" );
static const trait_id trait_THRESH_URSINE( "THRESH_URSINE" );
static const trait_id trait_UNDINE_SLEEP_WATER( "UNDINE_SLEEP_WATER" );
static const trait_id trait_VEGAN( "VEGAN" );
static const trait_id trait_VEGETARIAN( "VEGETARIAN" );
static const trait_id trait_WATERSLEEP( "WATERSLEEP" );
// note: cannot use constants from flag.h (e.g. flag_ALLERGEN_VEGGY) here, as they
// might be uninitialized at the time these const arrays are created
static const std::array<flag_id, 6> carnivore_blacklist {{
json_flag_ALLERGEN_VEGGY, json_flag_ALLERGEN_FRUIT,
json_flag_ALLERGEN_WHEAT, json_flag_ALLERGEN_NUT,
json_flag_ALLERGEN_BREAD, json_flag_ALLERGEN_DRIED_VEGETABLE
}};
static const std::array<flag_id, 2> herbivore_blacklist {{
json_flag_ALLERGEN_MEAT, json_flag_ALLERGEN_EGG
}};
// TODO: Move pizza scraping here.
static int compute_default_effective_kcal( const item &comest, const Character &you,
const cata::flat_set<flag_id> &extra_flags = {} )
{
if( !comest.get_comestible() ) {
return 0;
}
// As float to avoid rounding too many times
float kcal = comest.get_comestible()->default_nutrition.kcal();
// Many raw foods give less calories, as your body has expends more energy digesting them.
bool cooked = comest.has_flag( flag_COOKED ) || extra_flags.count( flag_COOKED );
if( comest.has_flag( flag_RAW ) && !cooked ) {
kcal *= 0.75f;
}
if( you.has_trait( trait_CARNIVORE ) && comest.has_flag( flag_CARNIVORE_OK ) &&
comest.has_any_flag( carnivore_blacklist ) ) {
// TODO: Comment pizza scrapping
kcal *= 0.5f;
}
const float relative_rot = comest.get_relative_rot();
// Saprophages get full nutrition from rotting food
if( relative_rot > 1.0f && !you.has_trait( trait_SAPROPHAGE ) ) {
// everyone else only gets a portion of the nutrition
// Scaling linearly from 100% at just-rotten to 0 at halfway-rotten-away
const float rottedness = clamp( 2 * relative_rot - 2.0f, 0.1f, 1.0f );
kcal *= ( 1.0f - rottedness );
}
kcal = you.enchantment_cache->modify_value( enchant_vals::mod::KCAL, kcal );
return static_cast<int>( kcal );
}
// Compute default effective vitamins for an item, taking into account player
// traits, but not components of the item.
static std::map<vitamin_id, int> compute_default_effective_vitamins(
const item &it, const Character &you )
{
if( !it.get_comestible() ) {
return {};
}
std::map<vitamin_id, int> res = it.get_comestible()->default_nutrition.vitamins();
// for actual vitamins convert RDA to a internal value
for( std::pair<const vitamin_id, int> &vit : res ) {
if( vit.first->type() != vitamin_type::VITAMIN ) {
continue;
}
vit.second = vit.first->RDA_to_default( vit.second );
}
for( const trait_id &trait : you.get_mutations() ) {
const mutation_branch &mut = trait.obj();
// make sure to iterate over every material defined for vitamin absorption
// TODO: put this loop into a function and utilize it again for bionics
for( const auto &mat : mut.vitamin_absorb_multi ) {
// this is where we are able to check if the food actually is changed by the trait
if( mat.first == material_all || it.made_of( mat.first ) ) {
const std::map<vitamin_id, double> &mat_vit_map = mat.second;
for( auto &vit : res ) {
auto vit_factor = mat_vit_map.find( vit.first );
if( vit_factor != mat_vit_map.end() ) {
vit.second *= vit_factor->second;
}
}
}
}
}
for( const bionic_id &bid : you.get_bionics() ) {
if( bid->vitamin_absorb_mod == 1.0f ) {
continue;
}
for( std::pair<const vitamin_id, int> &vit : res ) {
vit.second *= bid->vitamin_absorb_mod;
}
}
for( std::pair<const vitamin_id, int> &vit : res ) {
vit.second = you.enchantment_cache->modify_value( enchant_vals::mod::VITAMIN_ABSORB_MOD,
vit.second );
}
return res;
}
// Calculate the effective nutrients for a given item, taking
// into account character traits but not item components.
static nutrients compute_default_effective_nutrients( const item &comest,
const Character &you, const cata::flat_set<flag_id> &extra_flags = {} )
{
nutrients ret;
// Multiply by 1000 to get it in calories
ret.calories = compute_default_effective_kcal( comest, you, extra_flags ) * 1000;
for( const std::pair<const vitamin_id, int> &vit : compute_default_effective_vitamins( comest,
you ) ) {
ret.set_vitamin( vit.first, vit.second );
}
return ret;
}
// Calculate the nutrients that the given character would receive from consuming
// the given item, taking into account the item components and the character's
// traits.
// This is used by item display, making actual nutrition available to character.
nutrients Character::compute_effective_nutrients( const item &comest ) const
{
if( !comest.is_comestible() ) {
return {};
}
// if item has components, will derive calories from that instead.
if( !comest.components.empty() && !comest.has_flag( flag_NUTRIENT_OVERRIDE ) ) {
nutrients tally{};
if( comest.recipe_charges == 0 ) {
// Avoid division by zero
return tally;
}
for( const item_components::type_vector_pair &tvp : comest.components ) {
for( const item &component : tvp.second ) {
nutrients component_value = compute_effective_nutrients( component );
if( component.count_by_charges() ) {
component_value *= component.charges;
}
if( component.has_flag( flag_BYPRODUCT ) ) {
tally -= component_value;
} else {
tally += component_value;
}
}
}
return tally / comest.recipe_charges;
} else {
return compute_default_effective_nutrients( comest, *this );
}
}
// Calculate range of nutrients obtainable for a given item when crafted via
// the given recipe
std::pair<nutrients, nutrients> Character::compute_nutrient_range(
const item &comest, const recipe_id &recipe_i,
std::map<recipe_id, std::pair<nutrients, nutrients>> &rec_cache,
const cata::flat_set<flag_id> &extra_flags ) const
{
if( !comest.is_comestible() ) {
return {};
}
// if item has components, will derive calories from that instead.
if( comest.has_flag( flag_NUTRIENT_OVERRIDE ) ) {
nutrients result = compute_default_effective_nutrients( comest, *this );
return { result, result };
}
auto cache_it = rec_cache.find( recipe_i );
if( cache_it != rec_cache.end() ) {
return cache_it->second;
}
nutrients tally_min;
nutrients tally_max;
const recipe &rec = *recipe_i;
cata::flat_set<flag_id> our_extra_flags = extra_flags;
if( rec.hot_result() ) {
our_extra_flags.insert( flag_COOKED );
}
const requirement_data &requirements = rec.simple_requirements();
const requirement_data::alter_item_comp_vector &component_requirements =
requirements.get_components();
for( const std::vector<item_comp> &component_options : component_requirements ) {
nutrients this_min;
nutrients this_max;
bool first = true;
for( const item_comp &component_option : component_options ) {
std::pair<nutrients, nutrients> component_option_range =
compute_nutrient_range( component_option.type, rec_cache, our_extra_flags );
component_option_range.first *= component_option.count;
component_option_range.second *= component_option.count;
if( first ) {
std::tie( this_min, this_max ) = component_option_range;
first = false;
} else {
this_min.min_in_place( component_option_range.first );
this_max.max_in_place( component_option_range.second );
}
}
tally_min += this_min;
tally_max += this_max;
}
std::vector<item> byproducts = rec.create_byproducts();
for( const item &byproduct : byproducts ) {
nutrients byproduct_nutr = compute_default_effective_nutrients( byproduct, *this );
tally_min -= byproduct_nutr;
tally_max -= byproduct_nutr;
}
int count = rec.makes_amount();
rec_cache[recipe_i] = { tally_min / count, tally_max / count };
return rec_cache[recipe_i];
}
// Calculate the range of nutrients possible for a given item across all
// possible recipes
std::pair<nutrients, nutrients> Character::compute_nutrient_range(
const itype_id &comest_id,
std::map<recipe_id, std::pair<nutrients, nutrients>> &rec_cache,
const cata::flat_set<flag_id> &extra_flags ) const
{
const itype *comest = item::find_type( comest_id );
if( !comest->comestible ) {
return {};
}
item comest_it( comest, calendar::turn );
if( comest->count_by_charges() ) {
comest_it.charges = 1;
}
// The default nutrients are always a possibility
nutrients min_nutr = compute_default_effective_nutrients( comest_it, *this, extra_flags );
if( comest->has_flag( flag_NUTRIENT_OVERRIDE ) ||
recipe_dict.is_item_on_loop( comest->get_id() ) ) {
return { min_nutr, min_nutr };
}
nutrients max_nutr = min_nutr;
for( const recipe_id &rec : comest->recipes ) {
nutrients this_min;
nutrients this_max;
item result_it( rec->result() );
if( result_it.typeId() != comest_it.typeId() ) {
debugmsg( "When creating recipe result expected %s, got %s\n",
comest_it.typeId().str(), result_it.typeId().str() );
}
std::tie( this_min, this_max ) = compute_nutrient_range( result_it, rec, rec_cache, extra_flags );
min_nutr.min_in_place( this_min );
max_nutr.max_in_place( this_max );
}
return { min_nutr, max_nutr };
}
int Character::nutrition_for( const item &comest ) const
{
return compute_effective_nutrients( comest ).kcal() / islot_comestible::kcal_per_nutr;
}
std::pair<int, int> Character::fun_for( const item &comest, bool ignore_already_ate ) const
{
if( !comest.is_comestible() ) {
return std::pair<int, int>( 0, 0 );
}
// As float to avoid rounding too many times
float fun = comest.get_comestible_fun();
// Food doesn't taste as good when you're sick
if( ( has_effect( effect_common_cold ) || has_effect( effect_flu ) ) && fun > 0 ) {
fun /= 3;
}
// Rotten food should be pretty disgusting
const float relative_rot = comest.get_relative_rot();
if( relative_rot > 1.0f && !has_trait( trait_SAPROPHAGE ) && !has_trait( trait_SAPROVORE ) ) {
const float rottedness = clamp( 2 * relative_rot - 2.0f, 0.1f, 1.0f );
// Three effects:
// penalty for rot goes from -2 to -20
// bonus for tasty food drops from 90% to 0%
// disgusting food unfun increases from 110% to 200%
fun -= rottedness * 10;
if( fun > 0 ) {
fun *= ( 1.0f - rottedness );
} else {
fun *= ( 1.0f + rottedness );
}
}
// Food is less enjoyable when eaten too often.
if( !ignore_already_ate && ( fun > 0 || comest.has_flag( flag_NEGATIVE_MONOTONY_OK ) ) ) {
for( const consumption_event &event : consumption_history ) {
if( event.time > calendar::turn - 2_days && event.type_id == comest.typeId() &&
event.component_hash == comest.make_component_hash() ) {
fun -= comest.get_comestible()->monotony_penalty;
// This effect can't drop fun below 0, unless the food has the right flag.
// 0 is the lowest we'll go, no need to keep looping.
if( fun <= 0 && !comest.has_flag( flag_NEGATIVE_MONOTONY_OK ) ) {
fun = 0;
break;
}
}
}
}
float fun_max = fun < 0 ? fun * 6.0f : fun * 3.0f;
if( comest.has_flag( flag_EATEN_COLD ) && comest.has_flag( flag_COLD ) ) {
if( fun > 0 ) {
fun *= 2;
} else {
fun = 1;
fun_max = 5;
}
}
if( comest.has_flag( flag_MELTS ) && !comest.has_flag( flag_FROZEN ) ) {
if( fun > 0 ) {
fun *= 0.5;
} else {
// Melted freezable food tastes 25% worse than frozen freezable food.
// Frozen freezable food... say that 5 times fast
fun *= 1.25;
}
}
if( ( comest.has_flag( flag_LUPINE ) && has_trait( trait_THRESH_LUPINE ) ) ||
( comest.has_flag( flag_FELINE ) && has_trait( trait_THRESH_FELINE ) ) ) {
if( fun < 0 ) {
fun = -fun;
fun /= 2;
}
}
// Cooked blood is OK, but it's really better raw
// This is automatically handled by raw blood having negative fun
if( comest.has_flag( flag_HEMOVORE_FUN ) ) {
if( has_flag( json_flag_BLOODFEEDER ) ) {
if( fun <= 0 ) {
fun += 25;
} else {
fun *= 1.2;
}
} else if( has_flag( json_flag_HEMOVORE ) ) {
if( fun <= 0 ) {
fun += 13;
} else {
fun *= 1.1;
}
}
}
// This cherry soda's just not the same...
if( has_flag( json_flag_BLOODFEEDER ) && !comest.has_flag( flag_HEMOVORE_FUN ) && fun > 0 ) {
fun *= 0.5;
}
if( has_trait( trait_GOURMAND ) ) {
if( fun < -1 ) {
fun_max = fun;
fun /= 2;
} else if( fun > 0 ) {
fun_max *= 3;
fun = fun * 3 / 2;
}
}
if( has_bionic( bio_faulty_grossfood ) && comest.is_food() ) {
fun = fun - 13;
}
if( fun < 0 && has_active_bionic( bio_taste_blocker ) &&
get_power_level() > units::from_kilojoule( static_cast<std::int64_t>( std::abs(
comest.get_comestible_fun() ) ) ) ) {
fun = 0;
}
return { static_cast< int >( fun ), static_cast< int >( fun_max ) };
}
time_duration Character::vitamin_rate( const vitamin_id &vit ) const
{
time_duration res = vit.obj().rate();
for( const auto &m : get_mutations() ) {
const mutation_branch &mut = m.obj();
auto iter = mut.vitamin_rates.find( vit );
if( iter != mut.vitamin_rates.end() && iter->second != 0_turns ) {
if( res != 0_turns ) {
const float recip_vit = 1 / to_turns<float>( res ) + 1 / to_turns<float>( iter->second );
res = recip_vit == 0 ? 0_turns : time_duration::from_turns( 1 / recip_vit );
} else {
res = iter->second;
}
}
}
return res;
}
void Character::clear_vitamins()
{
vitamin_levels.clear();
daily_vitamins.clear();
for( const auto &[vitamin_id, vitamin] : vitamin::all() ) {
vitamin_levels[vitamin_id] = 0;
daily_vitamins[vitamin_id] = { 0, 0 };
}
}
std::map<vitamin_id, int> Character::effect_vitamin_mod( const std::map<vitamin_id, int> &vits )
{
std::vector<std::pair<vitamin_id, float>> mods;
// Yuck!
// Construct mods, for easy iteration over to modify vitamins
for( const std::pair<const efftype_id, std::map<bodypart_id, effect>> &elem : *effects ) {
for( const std::pair<const bodypart_id, effect> &veffect : elem.second ) {
const bool reduced = resists_effect( veffect.second );
for( const vitamin_applied_effect &rate_mod : veffect.second.vit_effects( reduced ) ) {
if( !rate_mod.absorb_mult ) {
continue;
}
mods.emplace_back( rate_mod.vitamin, *rate_mod.absorb_mult );
}
}
}
std::map<vitamin_id, int> ret = vits;
for( std::pair<const vitamin_id, int> &value : ret ) {
for( const std::pair<vitamin_id, float> &mod : mods ) {
if( value.first == mod.first ) {
value.second *= mod.second;
}
}
}
return ret;
}
int Character::vitamin_mod( const vitamin_id &vit, int qty )
{
if( !vit.is_valid() ) {
debugmsg( "Vitamin with id %s does not exist, and cannot be modified", vit.str() );
return 0;
}
// What's going on here? Emplace returns either an iterator to the inserted
// item or, if it already exists, an iterator to the (unchanged) extant item
// (Okay, technically it returns a pair<iterator, bool>, the iterator is what we want)
auto it = vitamin_levels.emplace( vit, 0 ).first;
const vitamin &v = *it->first;
if( qty > 0 ) {
it->second = std::min( it->second + qty, v.max() );
update_vitamins( vit );
// update the daily trackers too while here
// prevent overflow
constexpr int daily_vitamins_max = std::numeric_limits<int>::max();
if( daily_vitamins_max - daily_vitamins[vit].second >= qty ) {
daily_vitamins[vit].second += qty;
} else {
daily_vitamins[vit].second = daily_vitamins_max;
}
} else if( qty < 0 ) {
it->second = std::max( it->second + qty, v.min() );
update_vitamins( vit );
for( const std::pair<vitamin_id, int> &dcy : v.decays_into() ) {
if( it->second != 0 ) {
vitamin_mod( dcy.first, dcy.second * std::abs( qty ) );
}
}
}
return it->second;
}
void Character::vitamins_mod( const std::map<vitamin_id, int> &vitamins )
{
const bool npc_no_food = is_npc() && get_option<bool>( "NO_NPC_FOOD" );
if( !npc_no_food ) {
for( const std::pair<const vitamin_id, int> &vit : vitamins ) {
vitamin_mod( vit.first, vit.second );
}
}
}
int Character::vitamin_get( const vitamin_id &vit ) const
{
if( get_option<bool>( "NO_VITAMINS" ) && vit->type() == vitamin_type::VITAMIN ) {
return 0;
}
const auto &v = vitamin_levels.find( vit );
return v != vitamin_levels.end() ? v->second : 0;
}
int Character::get_daily_vitamin( const vitamin_id &vit, bool actual ) const
{
if( get_option<bool>( "NO_VITAMINS" ) && vit->type() == vitamin_type::VITAMIN ) {
return 0;
}
const auto &v = daily_vitamins.find( vit );
// we didn't find it
if( v == daily_vitamins.end() ) {
return 0;
}
// if we should get the guess or the real value
return actual ? v->second.second : v->second.first;
}
void Character::reset_daily_vitamin( const vitamin_id &vit )
{
if( get_option<bool>( "NO_VITAMINS" ) && vit->type() == vitamin_type::VITAMIN ) {
return;
}
daily_vitamins[vit] = { 0, 0 };
}
void Character::vitamin_set( const vitamin_id &vit, int qty )
{
auto v = vitamin_levels.find( vit );
if( v == vitamin_levels.end() ) {
v = vitamin_levels.emplace( vit, 0 ).first;
}
vitamin_mod( vit, qty - v->second );
}
float Character::metabolic_rate_base() const
{
static const std::string hunger_rate_string( "PLAYER_HUNGER_RATE" );
float hunger_rate = get_option< float >( hunger_rate_string );
static const std::string metabolism_modifier( "metabolism_modifier" );
return hunger_rate * ( 1.0f + mutation_value( metabolism_modifier ) );
}
// TODO: Make this less chaotic to let NPC retroactive catch up work here
// TODO: Involve body heat (cold -> higher metabolism, unless cold-blooded)
// TODO: Involve stamina (maybe not here?)
float Character::metabolic_rate() const
{
// First value is effective hunger, second is nutrition multiplier
// Note: Values do not match hungry/v.hungry/famished/starving,
// because effective hunger is affected by speed (which drops when hungry)
static const std::vector<std::pair<float, float>> thresholds = {{
{ 300.0f, 1.0f },
{ 2000.0f, 0.8f },
{ 5000.0f, 0.6f },
{ 8000.0f, 0.5f }
}
};
// Penalize fast survivors
// TODO: Have cold temperature increase, not decrease, metabolism
const float effective_hunger = ( get_hunger() + get_starvation() ) * 100.0f / std::max( 50,
get_speed() );
const float modifier = multi_lerp( thresholds, effective_hunger );
return modifier * metabolic_rate_base();
}
morale_type Character::allergy_type( const item &food ) const
{
using allergy_tuple = std::tuple<trait_id, flag_id, morale_type>;
static const std::array<allergy_tuple, 8> allergy_tuples = {{
std::make_tuple( trait_VEGETARIAN, flag_ALLERGEN_MEAT, MORALE_ANTIMEAT ),
std::make_tuple( trait_MEATARIAN, flag_ALLERGEN_VEGGY, MORALE_ANTIVEGGY ),
std::make_tuple( trait_LACTOSE, flag_ALLERGEN_MILK, MORALE_LACTOSE ),
std::make_tuple( trait_ANTIFRUIT, flag_ALLERGEN_FRUIT, MORALE_ANTIFRUIT ),
std::make_tuple( trait_ANTIJUNK, flag_ALLERGEN_JUNK, MORALE_ANTIJUNK ),
std::make_tuple( trait_ANTIWHEAT, flag_ALLERGEN_WHEAT, MORALE_ANTIWHEAT )
}
};
for( const auto &tp : allergy_tuples ) {
if( has_trait( std::get<0>( tp ) ) &&
food.has_flag( std::get<1>( tp ) ) ) {
return std::get<2>( tp );
}
}
return MORALE_NULL;
}
ret_val<edible_rating> Character::can_eat( const item &food ) const
{
if( !food.is_comestible() ) {
return ret_val<edible_rating>::make_failure( _( "That doesn't look edible." ) );
}
const auto &comest = food.get_comestible();
if( food.has_flag( flag_INEDIBLE ) ) {
bool has_compatible_mutation =
( food.has_flag( flag_CATTLE ) && has_trait( trait_THRESH_CATTLE ) ) ||
( food.has_flag( flag_FELINE ) && has_trait( trait_THRESH_FELINE ) ) ||
( food.has_flag( flag_LUPINE ) && has_trait( trait_THRESH_LUPINE ) ) ||
( food.has_flag( flag_RABBIT ) && has_trait( trait_THRESH_RABBIT ) ) ||
( food.has_flag( flag_MOUSE ) && has_trait( trait_THRESH_MOUSE ) ) ||
( food.has_flag( flag_RAT ) && has_trait( trait_THRESH_RAT ) ) ||
( food.has_flag( flag_BIRD ) && has_trait( trait_THRESH_BIRD ) );
if( !has_compatible_mutation ) {
return ret_val<edible_rating>::make_failure( _( "That doesn't look edible to you." ) );
}
if( !food.has_flag( flag_CATTLE ) && !food.has_flag( flag_FELINE ) &&
!food.has_flag( flag_LUPINE ) && !food.has_flag( flag_BIRD ) ) {
return ret_val<edible_rating>::make_failure( _( "That doesn't look edible." ) );
}
}
if( food.is_craft() ) {
return ret_val<edible_rating>::make_failure( _( "That doesn't look edible in its current form." ) );
}
if( food.has_own_flag( flag_DIRTY ) ) {
return ret_val<edible_rating>::make_failure(
_( "This is full of dirt after being on the ground." ) );
}
const bool eat_verb = food.has_flag( flag_USE_EAT_VERB );
const bool edible = eat_verb || comest->comesttype == comesttype_FOOD;
const bool drinkable = !eat_verb && comest->comesttype == comesttype_DRINK;
// TODO: This condition occurs way too often. Unify it.
// update Sep. 26 2018: this apparently still occurs way too often. yay!
if( is_underwater() && ( !has_trait( trait_WATERSLEEP ) ||
has_trait( trait_UNDINE_SLEEP_WATER ) ) ) {
return ret_val<edible_rating>::make_failure( _( "You can't do that while underwater." ) );
}
if( edible || drinkable ) {
for( const auto &elem : food.type->materials ) {
if( !elem.first->edible() ) {
return ret_val<edible_rating>::make_failure( _( "That doesn't look edible in its current form." ) );
}
}
// For all those folks who loved eating Marloss berries. D:< mwuhahaha
if( has_trait( trait_M_DEPENDENT ) && !food.has_flag( flag_MYCUS_OK ) ) {
return ret_val<edible_rating>::make_failure( INEDIBLE_MUTATION,
_( "We can't eat that. It's not right for us." ) );
}
}
if( food.has_own_flag( flag_FROZEN ) && !food.has_flag( flag_EDIBLE_FROZEN ) &&
!food.has_flag( flag_MELTS ) ) {
if( edible ) {
return ret_val<edible_rating>::make_failure(
_( "It's frozen solid. You must defrost it before you can eat it." ) );
}
if( drinkable ) {
return ret_val<edible_rating>::make_failure( _( "You can't drink it while it's frozen." ) );
}
}
const use_function *consume_drug = food.type->get_use( "consume_drug" );
if( consume_drug != nullptr ) { //its a drug)
const consume_drug_iuse *consume_drug_use = dynamic_cast<const consume_drug_iuse *>
( consume_drug->get_actor_ptr() );
for( const auto &tool : consume_drug_use->tools_needed ) {
const bool has = item::count_by_charges( tool.first )
? has_charges( tool.first, ( tool.second == -1 ) ? 1 : tool.second )
: has_amount( tool.first, 1 );
if( !has ) {
return ret_val<edible_rating>::make_failure( NO_TOOL,
string_format( _( "You need a %s to consume that!" ),
item::nname( tool.first ) ) );
}
}
}
const use_function *smoking = food.type->get_use( "SMOKING" );
if( smoking != nullptr ) {
std::optional<std::string> litcig = iuse::can_smoke( *this );
if( litcig.has_value() ) {
return ret_val<edible_rating>::make_failure( NO_TOOL, _( litcig.value_or( "" ) ) );
}
}
if( !comest->tool.is_null() ) {
const bool has = item::count_by_charges( comest->tool )
? has_charges( comest->tool, 1 )
: has_amount( comest->tool, 1 );
if( !has ) {
return ret_val<edible_rating>::make_failure( NO_TOOL,
string_format( _( "You need a %s to consume that!" ),
item::nname( comest->tool ) ) );
}
}
// Here's why PROBOSCIS is such a negative trait.
if( has_trait( trait_PROBOSCIS ) && !( drinkable || food.is_medication() ) ) {
return ret_val<edible_rating>::make_failure( INEDIBLE_MUTATION, _( "Ugh, you can't drink that!" ) );
}
if( has_trait( trait_CARNIVORE ) && nutrition_for( food ) > 0 &&
food.has_any_flag( carnivore_blacklist ) && !food.has_flag( flag_CARNIVORE_OK ) ) {
return ret_val<edible_rating>::make_failure( INEDIBLE_MUTATION,
_( "Eww. Inedible plant stuff!" ) );
}
if( ( has_trait( trait_HERBIVORE ) || has_trait( trait_RUMINANT ) ) &&
food.has_any_flag( herbivore_blacklist ) ) {
// Like non-cannibal, but more strict!
return ret_val<edible_rating>::make_failure( INEDIBLE_MUTATION,
_( "The thought of eating that makes you feel sick." ) );
}
const std::array<flag_id, 6> vegan_blacklist {{
json_flag_ALLERGEN_MEAT, json_flag_ALLERGEN_EGG,
json_flag_ALLERGEN_MILK, json_flag_ANIMAL_PRODUCT,
json_flag_ALLERGEN_CHEESE, flag_URSINE_HONEY
}};
if( has_trait( trait_VEGAN ) &&
food.has_any_flag( vegan_blacklist ) ) {
return ret_val<edible_rating>::make_failure( INEDIBLE_MUTATION,
_( "You're still not going to eat animal products." ) );
}
for( const trait_id &mut : get_mutations() ) {
if( !food.made_of_any( mut.obj().can_only_eat ) && !mut.obj().can_only_eat.empty() ) {
return ret_val<edible_rating>::make_failure( INEDIBLE_MUTATION, _( "You can't eat this." ) );
}
}
if( has_trait( trait_PICKYEATER ) && !( drinkable || food.is_medication() ) &&
fun_for( food ).first < 0 && !has_effect( effect_hunger_near_starving ) &&
!has_effect( effect_hunger_starving ) && !has_effect( effect_hunger_famished ) ) {
return ret_val<edible_rating>::make_failure( INEDIBLE_MUTATION,
_( "You deserve better food than this." ) );
}
if( has_trait( trait_PICKYEATER ) && drinkable && !food.is_medication() &&
fun_for( food ).first < 0 && get_thirst() < 240 ) {
return ret_val<edible_rating>::make_failure( INEDIBLE_MUTATION,
_( "You are not going to drink this." ) );
}
return ret_val<edible_rating>::make_success();
}
ret_val<edible_rating> Character::will_eat( const item &food, bool interactive ) const
{
ret_val<edible_rating> ret = can_eat( food );
if( !ret.success() ) {
if( interactive ) {
add_msg_if_player( m_info, "%s", ret.c_str() );
}
return ret;
}
// exit early as we've already tested everything in can_eat
if( !food.is_comestible() ) {
return ret_val<edible_rating>::make_success();
}
std::vector<ret_val<edible_rating>> consequences;
const auto add_consequence = [&consequences]( const std::string & msg, edible_rating code ) {
consequences.emplace_back( ret_val<edible_rating>::make_failure( code, msg ) );
};
const bool saprophage = has_trait( trait_SAPROPHAGE );
const auto &comest = food.get_comestible();
if( food.rotten() ) {
const bool saprovore = has_trait( trait_SAPROVORE );
if( !saprophage && !saprovore ) {
add_consequence( _( "This is rotten and smells awful!" ), ROTTEN );
}
}
const bool carnivore = has_trait( trait_CARNIVORE );
const bool food_is_human_flesh = food.has_flag( flag_CANNIBALISM ) ||
( food.has_flag( flag_STRICT_HUMANITARIANISM ) &&
!has_flag( json_flag_STRICT_HUMANITARIAN ) );
if( ( food_is_human_flesh && !has_flag( STATIC( json_character_flag( "CANNIBAL" ) ) ) &&
!has_flag( json_flag_PSYCHOPATH ) && !has_flag( json_flag_SAPIOVORE ) ) &&
( !food.has_flag( flag_HEMOVORE_FUN ) || ( !has_flag( json_flag_BLOODFEEDER ) ) ) ) {
add_consequence( _( "The thought of eating human flesh makes you feel sick." ), CANNIBALISM );
}
if( food.get_comestible()->parasites > 0 && !food.has_flag( flag_NO_PARASITES ) &&
!has_flag( json_flag_PARAIMMUNE ) && ( !food.has_flag( flag_HEMOVORE_FUN ) ||
( !has_flag( json_flag_HEMOVORE ) && !has_flag( json_flag_BLOODFEEDER ) ) ) ) {
add_consequence( string_format( _( "Consuming this %s probably isn't very healthy." ),
food.tname() ),
PARASITES );
}
const bool edible = comest->comesttype == comesttype_FOOD || food.has_flag( flag_USE_EAT_VERB );
if( edible && has_effect( effect_nausea ) ) {
add_consequence( _( "You still feel nauseous and will probably puke it all up again." ), NAUSEA );
}
if( ( allergy_type( food ) != MORALE_NULL ) || ( carnivore && food.has_flag( flag_ALLERGEN_JUNK ) &&
!food.has_flag( flag_CARNIVORE_OK ) ) ) {
add_consequence( _( "Your stomach won't be happy (allergy)." ), ALLERGY );
}
if( saprophage && edible && !food.rotten() && !food.has_flag( flag_FERTILIZER ) ) {
// Note: We're allowing all non-solid "food". This includes drugs
// Hard-coding fertilizer for now - should be a separate flag later
//~ No, we don't eat "rotten" food. We eat properly aged food, like a normal person.
//~ Semantic difference, but greatly facilitates people being proud of their character.
add_consequence( _( "Your stomach won't be happy (not rotten enough)." ), ALLERGY_WEAK );
}
if( food.is_food() &&
( food.charges_per_volume( stomach.stomach_remaining( *this ) ) < 1 ||
has_effect( effect_hunger_full ) || has_effect( effect_hunger_engorged ) ) ) {
if( edible ) {
add_consequence( _( "You're full already and will be forcing yourself to eat." ), TOO_FULL );
} else {
add_consequence( _( "You're full already and will be forcing yourself to drink." ), TOO_FULL );
}
}
if( !consequences.empty() ) {
if( !interactive ) {
return consequences.front();
}
std::string req;
for( const auto &elem : consequences ) {
req += elem.str() + "\n";
}