forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bionics.cpp
2957 lines (2696 loc) · 118 KB
/
bionics.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 "bionics.h"
#include <algorithm> //std::min
#include <array>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <forward_list>
#include <iterator>
#include <list>
#include <memory>
#include <optional>
#include <type_traits>
#include "action.h"
#include "assign.h"
#include "avatar.h"
#include "avatar_action.h"
#include "ballistics.h"
#include "calendar.h"
#include "cata_utility.h"
#include "character.h"
#include "character_martial_arts.h"
#include "character_stat.h"
#include "colony.h"
#include "color.h"
#include "consistency_report.h"
#include "cursesdef.h"
#include "damage.h"
#include "debug.h"
#include "dispersion.h"
#include "effect.h"
#include "enum_conversions.h"
#include "enums.h"
#include "event.h"
#include "event_bus.h"
#include "explosion.h"
#include "field_type.h"
#include "game.h"
#include "generic_factory.h"
#include "handle_liquid.h"
#include "input.h"
#include "int_id.h"
#include "item.h"
#include "item_functions.h"
#include "item_location.h"
#include "itype.h"
#include "json.h"
#include "line.h"
#include "magic.h"
#include "make_static.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "memorial_logger.h"
#include "messages.h"
#include "monster.h"
#include "morale_types.h"
#include "mutation.h"
#include "npc.h"
#include "options.h"
#include "output.h"
#include "overmapbuffer.h"
#include "pimpl.h"
#include "player.h"
#include "player_activity.h"
#include "pldata.h"
#include "point.h"
#include "projectile.h"
#include "requirements.h"
#include "rng.h"
#include "sounds.h"
#include "string_formatter.h"
#include "string_id.h"
#include "teleport.h"
#include "translations.h"
#include "ui.h"
#include "ui_manager.h"
#include "units.h"
#include "units_utility.h"
#include "value_ptr.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "weather.h"
#include "weather_gen.h"
static const activity_id ACT_OPERATION( "ACT_OPERATION" );
static const efftype_id effect_adrenaline( "adrenaline" );
static const efftype_id effect_assisted( "assisted" );
static const efftype_id effect_asthma( "asthma" );
static const efftype_id effect_bleed( "bleed" );
static const efftype_id effect_bloodworms( "bloodworms" );
static const efftype_id effect_cig( "cig" );
static const efftype_id effect_cocaine_high( "cocaine_high" );
static const efftype_id effect_datura( "datura" );
static const efftype_id effect_dermatik( "dermatik" );
static const efftype_id effect_drunk( "drunk" );
static const efftype_id effect_fungus( "fungus" );
static const efftype_id effect_hallu( "hallu" );
static const efftype_id effect_heating_bionic( "heating_bionic" );
static const efftype_id effect_iodine( "iodine" );
static const efftype_id effect_mending( "mending" );
static const efftype_id effect_meth( "meth" );
static const efftype_id effect_narcosis( "narcosis" );
static const efftype_id effect_operating( "operating" );
static const efftype_id effect_pblue( "pblue" );
static const efftype_id effect_pkill_l( "pkill_l" );
static const efftype_id effect_pkill1( "pkill1" );
static const efftype_id effect_pkill2( "pkill2" );
static const efftype_id effect_pkill3( "pkill3" );
static const efftype_id effect_poison( "poison" );
static const efftype_id effect_badpoison( "badpoison" );
static const efftype_id effect_sleep( "sleep" );
static const efftype_id effect_stung( "stung" );
static const efftype_id effect_teleglow( "teleglow" );
static const efftype_id effect_took_flumed( "took_flumed" );
static const efftype_id effect_took_prozac( "took_prozac" );
static const efftype_id effect_took_prozac_bad( "took_prozac_bad" );
static const efftype_id effect_took_xanax( "took_xanax" );
static const efftype_id effect_under_op( "under_operation" );
static const efftype_id effect_visuals( "visuals" );
static const efftype_id effect_weed_high( "weed_high" );
static const efftype_id effect_infected( "infected" );
static const itype_id fuel_type_battery( "battery" );
static const itype_id fuel_type_metabolism( "metabolism" );
static const itype_id fuel_type_muscle( "muscle" );
static const itype_id fuel_type_sun_light( "sunlight" );
static const itype_id fuel_type_wind( "wind" );
static const itype_id itype_adv_UPS_off( "adv_UPS_off" );
static const itype_id itype_anesthetic( "anesthetic" );
static const itype_id itype_burnt_out_bionic( "burnt_out_bionic" );
static const itype_id itype_radiocontrol( "radiocontrol" );
static const itype_id itype_remotevehcontrol( "remotevehcontrol" );
static const itype_id itype_UPS_off( "UPS_off" );
static const itype_id itype_water_clean( "water_clean" );
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 skill_id skill_mechanics( "mechanics" );
static const bionic_id bio_adrenaline( "bio_adrenaline" );
static const bionic_id bio_advreactor( "bio_advreactor" );
static const bionic_id bio_ads( "bio_ads" );
static const bionic_id bio_blood_anal( "bio_blood_anal" );
static const bionic_id bio_blood_filter( "bio_blood_filter" );
static const bionic_id bio_cqb( "bio_cqb" );
static const bionic_id bio_earplugs( "bio_earplugs" );
static const bionic_id bio_ears( "bio_ears" );
static const bionic_id bio_emp( "bio_emp" );
static const bionic_id bio_evap( "bio_evap" );
static const bionic_id bio_eye_optic( "bio_eye_optic" );
static const bionic_id bio_flashbang( "bio_flashbang" );
static const bionic_id bio_geiger( "bio_geiger" );
static const bionic_id bio_gills( "bio_gills" );
static const bionic_id bio_hydraulics( "bio_hydraulics" );
static const bionic_id bio_jointservo( "bio_jointservo" );
static const bionic_id bio_lighter( "bio_lighter" );
static const bionic_id bio_lockpick( "bio_lockpick" );
static const bionic_id bio_magnet( "bio_magnet" );
static const bionic_id bio_meteorologist( "bio_meteorologist" );
static const bionic_id bio_nanobots( "bio_nanobots" );
static const bionic_id bio_painkiller( "bio_painkiller" );
static const bionic_id bio_power_storage( "bio_power_storage" );
static const bionic_id bio_power_storage_mkII( "bio_power_storage_mkII" );
static const bionic_id bio_probability_travel( "bio_probability_travel" );
static const bionic_id bio_radscrubber( "bio_radscrubber" );
static const bionic_id bio_reactor( "bio_reactor" );
static const bionic_id bio_remote( "bio_remote" );
static const bionic_id bio_resonator( "bio_resonator" );
static const bionic_id bio_shockwave( "bio_shockwave" );
static const bionic_id bio_teleport( "bio_teleport" );
static const bionic_id bio_time_freeze( "bio_time_freeze" );
static const bionic_id bio_tools( "bio_tools" );
static const bionic_id bio_torsionratchet( "bio_torsionratchet" );
static const bionic_id bio_water_extractor( "bio_water_extractor" );
static const bionic_id bionic_TOOLS_EXTEND( "bio_tools_extend" );
static const bionic_id afs_bio_dopamine_stimulators( "afs_bio_dopamine_stimulators" );
static const trait_id trait_CENOBITE( "CENOBITE" );
static const trait_id trait_DEBUG_BIONICS( "DEBUG_BIONICS" );
static const trait_id trait_MASOCHIST( "MASOCHIST" );
static const trait_id trait_MASOCHIST_MED( "MASOCHIST_MED" );
static const trait_id trait_NOPAIN( "NOPAIN" );
static const trait_id trait_PROF_AUTODOC( "PROF_AUTODOC" );
static const trait_id trait_PROF_MED( "PROF_MED" );
static const trait_id trait_REGEN_LIZ( "REGEN_LIZ" );
static const trait_id trait_THRESH_MEDICAL( "THRESH_MEDICAL" );
static const std::string flag_ALLOWS_NATURAL_ATTACKS( "ALLOWS_NATURAL_ATTACKS" );
static const std::string flag_AURA( "AURA" );
static const std::string flag_CABLE_SPOOL( "CABLE_SPOOL" );
static const std::string flag_NO_UNWIELD( "NO_UNWIELD" );
static const std::string flag_PERPETUAL( "PERPETUAL" );
static const std::string flag_PERSONAL( "PERSONAL" );
static const std::string flag_SAFE_FUEL_OFF( "SAFE_FUEL_OFF" );
static const std::string flag_SEALED( "SEALED" );
static const std::string flag_SEMITANGIBLE( "SEMITANGIBLE" );
static const std::string flag_SPLINT( "SPLINT" );
static const flag_str_id flag_BIONIC_GUN( "BIONIC_GUN" );
static const flag_str_id flag_BIONIC_WEAPON( "BIONIC_WEAPON" );
static const flag_str_id flag_BIONIC_TOGGLED( "BIONIC_TOGGLED" );
// (De-)Installation difficulty for bionics that don't have item form
constexpr int BIONIC_NOITEM_DIFFICULTY = 12;
namespace
{
generic_factory<bionic_data> bionic_factory( "bionic" );
std::vector<bionic_id> faulty_bionics;
} //namespace
/** @relates string_id */
template<>
const bionic_data &string_id<bionic_data>::obj() const
{
return bionic_factory.obj( *this );
}
/** @relates string_id */
template<>
bool string_id<bionic_data>::is_valid() const
{
return bionic_factory.is_valid( *this );
}
std::vector<bodypart_id> get_occupied_bodyparts( const bionic_id &bid )
{
std::vector<bodypart_id> parts;
for( const std::pair<const bodypart_str_id, int> &element : bid->occupied_bodyparts ) {
if( element.second > 0 ) {
parts.push_back( element.first.id() );
}
}
return parts;
}
bool bionic_data::has_flag( const flag_str_id &flag ) const
{
return flags.count( flag ) > 0;
}
itype_id bionic_data::itype() const
{
// Item id matches bionic id (as strings).
return itype_id( id.str() );
}
bool bionic_data::is_included( const bionic_id &id ) const
{
return std::find( included_bionics.begin(), included_bionics.end(), id ) != included_bionics.end();
}
void bionic_data::load_bionic( const JsonObject &jo, const std::string &src )
{
bionic_factory.load( jo, src );
}
void bionic_data::check_consistency()
{
bionic_factory.check();
}
void bionic_data::finalize_all()
{
bionic_factory.finalize();
for( const bionic_data &bd : bionic_factory.get_all() ) {
bd.finalize();
}
}
void bionic_data::reset()
{
bionic_factory.reset();
faulty_bionics.clear();
}
void bionic_data::load( const JsonObject &jsobj, const std::string src )
{
const bool strict = is_strict_enabled( src );
mandatory( jsobj, was_loaded, "name", name );
mandatory( jsobj, was_loaded, "description", description );
assign( jsobj, "act_cost", power_activate, strict, 0_kJ );
assign( jsobj, "deact_cost", power_deactivate, strict, 0_kJ );
assign( jsobj, "react_cost", power_over_time, strict, 0_kJ );
assign( jsobj, "trigger_cost", power_trigger, strict, 0_kJ );
assign( jsobj, "kcal_trigger_cost", kcal_trigger, strict, 0 );
assign( jsobj, "time", charge_time, strict, 0 );
assign( jsobj, "capacity", capacity, strict, 0_kJ );
assign( jsobj, "included", included, strict );
assign( jsobj, "weight_capacity_modifier", weight_capacity_modifier, strict, 1.0f );
assign( jsobj, "weight_capacity_bonus", weight_capacity_bonus, strict, 0_gram );
assign_map_from_array( jsobj, "stat_bonus", stat_bonus, strict );
assign( jsobj, "is_remote_fueled", is_remote_fueled, strict );
assign( jsobj, "fuel_options", fuel_opts, strict );
assign( jsobj, "fuel_capacity", fuel_capacity, strict, 0 );
assign( jsobj, "fuel_efficiency", fuel_efficiency, strict, 0.0f );
assign( jsobj, "fuel_multiplier", fuel_multiplier, strict, 0 );
assign( jsobj, "passive_fuel_efficiency", passive_fuel_efficiency, strict, 0.0f );
assign( jsobj, "coverage_power_gen_penalty", coverage_power_gen_penalty, strict );
assign( jsobj, "exothermic_power_gen", exothermic_power_gen, strict );
assign( jsobj, "power_gen_emission", power_gen_emission, strict );
assign_map_from_array( jsobj, "env_protec", env_protec, strict );
assign_map_from_array( jsobj, "bash_protec", bash_protec, strict );
assign_map_from_array( jsobj, "cut_protec", cut_protec, strict );
assign_map_from_array( jsobj, "bullet_protec", bullet_protec, strict );
assign_map_from_array( jsobj, "occupied_bodyparts", occupied_bodyparts, strict );
assign_map_from_array( jsobj, "encumbrance", encumbrance, strict );
assign( jsobj, "fake_item", fake_item, strict );
assign( jsobj, "canceled_mutations", canceled_mutations, strict );
assign( jsobj, "enchantments", enchantments, strict );
assign_map_from_array( jsobj, "learned_spells", learned_spells, strict );
assign( jsobj, "included_bionics", included_bionics, strict );
assign( jsobj, "upgraded_bionic", upgraded_bionic, strict );
assign( jsobj, "available_upgrades", available_upgrades, strict );
assign( jsobj, "flags", flags, strict );
activated = has_flag( flag_BIONIC_TOGGLED ) ||
power_activate > 0_kJ ||
charge_time > 0;
}
void bionic_data::finalize() const
{
if( has_flag( STATIC( flag_str_id( "BIONIC_FAULTY" ) ) ) ) {
faulty_bionics.push_back( id );
}
}
void bionic_data::check() const
{
consistency_report rep;
if( !included && !itype().is_valid() ) {
rep.warn( "has no defined item version" );
}
if( charge_time < 0 ) {
rep.warn( "specifies charge_time < 0" );
}
for( const itype_id &it : fuel_opts ) {
if( !it.is_valid() ) {
rep.warn( "specifies as fuel option unknown item \"%s\"", it );
}
}
if( power_gen_emission && !power_gen_emission.is_valid() ) {
rep.warn( "specifies unknown emission \"%s\"", power_gen_emission.str() );
}
for( const auto &it : env_protec ) {
if( !it.first.is_valid() ) {
rep.warn( "env_protec specifies unknown body part \"%s\"", it.first.str() );
}
}
for( const auto &it : cut_protec ) {
if( !it.first.is_valid() ) {
rep.warn( "cut_protec specifies unknown body part \"%s\"", it.first.str() );
}
}
for( const auto &it : bullet_protec ) {
if( !it.first.is_valid() ) {
rep.warn( "bullet_protec specifies unknown body part \"%s\"", it.first.str() );
}
}
for( const auto &it : bash_protec ) {
if( !it.first.is_valid() ) {
rep.warn( "bash_protec specifies unknown body part \"%s\"", it.first.str() );
}
}
for( const enchantment_id &eid : id->enchantments ) {
if( !eid.is_valid() ) {
rep.warn( "uses undefined enchantment \"%s\"", eid.str() );
}
}
for( const auto &it : occupied_bodyparts ) {
if( !it.first.is_valid() ) {
rep.warn( "occupies unknown body part \"%s\"", it.first.str() );
}
}
for( const auto &it : encumbrance ) {
if( !it.first.is_valid() ) {
rep.warn( "encumbers unknown body part \"%s\"", it.first.str() );
}
}
if( !fake_item.is_empty() && !fake_item.is_valid() ) {
rep.warn( "has unknown fake_item \"%s\"", fake_item );
}
for( const trait_id &mid : canceled_mutations ) {
if( !mid.is_valid() ) {
rep.warn( "cancels undefined mutation \"%s\"", mid.str() );
}
}
for( const auto &it : learned_spells ) {
if( !it.first.is_valid() ) {
rep.warn( "teaches unknown spell \"%s\"", it.first.str() );
}
}
for( const bionic_id &bid : included_bionics ) {
if( !bid.is_valid() ) {
rep.warn( "includes undefined bionic \"%s\"", bid.str() );
}
if( !bid->occupied_bodyparts.empty() ) {
rep.warn( "includes bionic \"%s\" which occupies slots. Those slots should be occupied by this bionic instead.",
bid.str() );
}
}
if( upgraded_bionic ) {
if( upgraded_bionic == id ) {
rep.warn( "is upgraded with itself" );
} else if( !upgraded_bionic.is_valid() ) {
rep.warn( "upgrades undefined bionic \"%s\"", upgraded_bionic.str() );
}
}
for( const bionic_id &it : available_upgrades ) {
if( !it.is_valid() ) {
rep.warn( "specifies unknown upgrade \"%s\"", it.str() );
}
}
for( const flag_str_id &it : flags ) {
if( !it.is_valid() ) {
rep.warn( "specifies unknown flag \"%s\"", it.str() );
}
}
if( has_flag( flag_BIONIC_GUN ) && has_flag( flag_BIONIC_WEAPON ) ) {
rep.warn( "is specified as both gun and weapon bionic" );
}
if( ( has_flag( flag_BIONIC_GUN ) || has_flag( flag_BIONIC_WEAPON ) ) && fake_item.is_empty() ) {
rep.warn( "is missing fake_item" );
}
if( !rep.is_empty() ) {
debugmsg( rep.format( "bionic", id.str() ) );
}
}
bionic_data::bionic_data() : name( no_translation( "bad bionic" ) ),
description( no_translation( "This bionic was not set up correctly, this is a bug" ) )
{
}
bionic_data::~bionic_data() = default;
static void force_comedown( effect &eff )
{
if( eff.is_null() || eff.get_effect_type() == nullptr || eff.get_duration() <= 1_turns ) {
return;
}
eff.set_duration( std::min( eff.get_duration(), eff.get_int_dur_factor() ) );
}
void npc::discharge_cbm_weapon()
{
if( cbm_active.is_null() ) {
return;
}
mod_power_level( -cbm_active->power_activate );
cbm_fake_active = null_item_reference();
cbm_active = bionic_id::NULL_ID();
}
void deactivate_weapon_cbm( npc &who )
{
for( bionic &i : *who.my_bionics ) {
if( i.powered && i.info().has_flag( flag_BIONIC_WEAPON ) ) {
who.deactivate_bionic( i );
}
}
who.clear_npc_ai_info_cache( npc_ai_info::ideal_weapon_value );
}
std::vector<std::pair<bionic_id, item>> find_reloadable_cbms( npc &who )
{
std::vector<std::pair<bionic_id, item>> cbm_list;
// Runs down full list of CBMs that qualify as weapons.
// Need a way to make this less costly.
for( bionic bio : *who.my_bionics ) {
if( !bio.info().has_flag( flag_BIONIC_WEAPON ) ) {
continue;
}
item cbm_fake = item( bio.info().fake_item );
// I'd hope it's not possible to be greater than but...
if( static_cast<int>( bio.ammo_count ) >= cbm_fake.ammo_capacity() ) {
continue;
}
if( bio.ammo_count > 0 ) {
cbm_fake.ammo_set( bio.ammo_loaded, bio.ammo_count );
}
cbm_list.emplace_back( std::make_pair( bio.id, cbm_fake ) );
}
return cbm_list;
}
const std::map<item, bionic_id> npc::check_toggle_cbm()
{
std::map<item, bionic_id> res;
const float allowed_ratio = static_cast<int>( rules.cbm_reserve ) / 100.0f;
const units::energy free_power = get_power_level() - get_max_power_level() * allowed_ratio;
if( free_power <= 0_J ) {
return res;
}
for( bionic &bio : *my_bionics ) {
// I'm not checking if NPC_USABLE because if it isn't it shouldn't be in them.
if( bio.powered || !bio.info().has_flag( flag_BIONIC_WEAPON ) ||
free_power < bio.info().power_activate ) {
continue;
}
item cbm_fake = item( bio.info().fake_item );
if( bio.ammo_count > 0 ) {
cbm_fake.ammo_set( bio.ammo_loaded, bio.ammo_count );
}
res[cbm_fake] = bio.id;
}
return res;
}
void npc::check_or_use_weapon_cbm()
{
// if active bionics have been chosen, keep using them.
if( !cbm_active.is_null() ) {
return;
}
std::vector<int> avail_active_cbms;
const float allowed_ratio = static_cast<int>( rules.cbm_reserve ) / 100.0f;
const units::energy free_power = get_power_level() - get_max_power_level() * allowed_ratio;
if( free_power <= 0_J ) {
return;
}
int cbm_index = 0;
for( bionic &bio : *my_bionics ) {
// I'm not checking if NPC_USABLE because if it isn't it shouldn't be in them.
if( free_power >= bio.info().power_activate && bio.info().has_flag( flag_BIONIC_GUN ) ) {
avail_active_cbms.push_back( cbm_index );
}
cbm_index++;
}
if( !avail_active_cbms.empty() ) {
Creature *critter = current_target();
int dist = rl_dist( pos(), critter->pos() );
int active_index = -1;
int best_dps = -1;
bool wield_gun = primary_weapon().is_gun();
item best_cbm_active = null_item_reference();
for( int i : avail_active_cbms ) {
bionic &bio = ( *my_bionics )[ i ];
const item cbm_weapon = item( bio.info().fake_item );
bool not_allowed = !rules.has_flag( ally_rule::use_guns ) ||
( rules.has_flag( ally_rule::use_silent ) && !cbm_weapon.is_silent() );
if( is_player_ally() && not_allowed ) {
continue;
}
auto [mode_id, mode_] = npc_ai::best_mode_for_range( *this, cbm_weapon, dist );
double dps = cbm_weapon.ideal_ranged_dps( *this, mode_ );
if( wield_gun && active_index < 0 ) {
gun_mode weap_mode = npc_ai::best_mode_for_range( *this, this->primary_weapon(), dist ).second;
dps = this->primary_weapon().ideal_ranged_dps( *this, weap_mode );
}
if( ( !wield_gun && active_index < 0 ) || dps > best_dps ) {
active_index = i;
best_cbm_active = cbm_weapon;
best_dps = dps;
}
}
if( active_index > 0 ) {
cbm_active = ( *my_bionics )[active_index].id;
cbm_fake_active = best_cbm_active;
}
}
}
// Why put this in a Big Switch? Why not let bionics have pointers to
// functions, much like monsters and items?
//
// Well, because like diseases, which are also in a Big Switch, bionics don't
// share functions....
bool Character::activate_bionic( bionic &bio, bool eff_only )
{
const bool mounted = is_mounted();
if( bio.incapacitated_time > 0_turns ) {
add_msg_if_player( m_info, _( "Your %s is shorting out and can't be activated." ),
bio.info().name );
return false;
}
// eff_only means only do the effect without messing with stats or displaying messages
if( !eff_only ) {
if( bio.powered ) {
// It's already on!
return false;
}
if( !enough_power_for( bio.id ) ) {
add_msg_if_player( m_info, _( "You don't have the power to activate your %s." ),
bio.info().name );
return false;
}
// HACK: burn_fuel() doesn't check for available fuel in remote source on start.
// If CBM is successfully activated, the check will occur when it actually tries to draw power
if( !bio.info().is_remote_fueled ) {
if( !burn_fuel( bio, true ) ) {
return false;
}
}
// We can actually activate now, do activation-y things
mod_power_level( -bio.info().power_activate );
bio.powered = bio.info().has_flag( flag_BIONIC_TOGGLED ) || bio.info().charge_time > 0;
if( bio.info().charge_time > 0 ) {
bio.charge_timer = bio.info().charge_time;
}
if( !bio.id->enchantments.empty() ) {
recalculate_enchantment_cache();
}
}
auto add_msg_activate = [&]() {
if( !eff_only && !bio.is_auto_start_keep_full() ) {
add_msg_if_player( m_info, _( "You activate your %s." ), bio.info().name );
} else if( get_player_character().sees( pos() ) ) {
add_msg( m_info, _( "%s activates their %s." ), disp_name(),
bio.info().name );
}
};
auto refund_power = [&]() {
if( !eff_only ) {
mod_power_level( bio.info().power_activate );
}
};
item tmp_item;
const w_point &weatherPoint = get_weather().get_precise();
map &here = get_map();
// On activation effects go here
if( bio.info().has_flag( flag_BIONIC_GUN ) ) {
add_msg_activate();
refund_power(); // Power usage calculated later, in avatar_action::fire
avatar_action::fire_ranged_bionic( *this->as_avatar(), item( bio.info().fake_item ),
bio.info().power_activate );
} else if( bio.info().has_flag( flag_BIONIC_WEAPON ) ) {
if( primary_weapon().has_flag( flag_NO_UNWIELD ) ) {
add_msg_if_player( m_info, _( "Deactivate your %s first!" ), primary_weapon().tname() );
refund_power();
bio.powered = false;
return false;
}
if( !primary_weapon().is_null() ) {
const std::string query = string_format( _( "Stop wielding %s?" ), primary_weapon().tname() );
if( !dispose_item( item_location( *this, &primary_weapon() ), query ) ) {
refund_power();
bio.powered = false;
return false;
}
}
add_msg_activate();
primary_weapon() = item( bio.info().fake_item );
primary_weapon().invlet = '#';
if( is_player() && bio.ammo_count > 0 ) {
primary_weapon().ammo_set( bio.ammo_loaded, bio.ammo_count );
avatar_action::fire_wielded_weapon( g->u );
}
clear_npc_ai_info_cache( npc_ai_info::ideal_weapon_value );
} else if( bio.id == bio_ears && has_active_bionic( bio_earplugs ) ) {
add_msg_activate();
for( bionic &bio : *my_bionics ) {
if( bio.id == bio_earplugs ) {
bio.powered = false;
add_msg_if_player( m_info, _( "Your %s automatically turn off." ),
bio.info().name );
}
}
} else if( bio.id == bio_earplugs && has_active_bionic( bio_ears ) ) {
add_msg_activate();
for( bionic &bio : *my_bionics ) {
if( bio.id == bio_ears ) {
bio.powered = false;
add_msg_if_player( m_info, _( "Your %s automatically turns off." ),
bio.info().name );
}
}
} else if( bio.id == bio_evap ) {
add_msg_activate();
const w_point &weatherPoint = get_weather().get_precise();
int humidity = get_local_humidity( weatherPoint.humidity, get_weather().weather_id,
g->is_sheltered( g->u.pos() ) );
// thirst units = 5 mL
int water_available = std::lround( humidity * 3.0 / 100.0 );
if( water_available == 0 ) {
bio.powered = false;
add_msg_if_player( m_bad, _( "There is not enough humidity in the air for your %s to function." ),
bio.info().name );
return false;
} else if( water_available == 1 ) {
add_msg_if_player( m_mixed,
_( "Your %s issues a low humidity warning. Efficiency will be reduced." ),
bio.info().name );
}
} else if( bio.id == bio_tools ) {
add_msg_activate();
invalidate_crafting_inventory();
} else if( bio.id == bio_cqb ) {
add_msg_activate();
const avatar *you = as_avatar();
if( you && !martial_arts_data->pick_style( *you ) ) {
bio.powered = false;
add_msg_if_player( m_info, _( "You change your mind and turn it off." ) );
return false;
}
} else if( bio.id == bio_resonator ) {
add_msg_activate();
//~Sound of a bionic sonic-resonator shaking the area
sounds::sound( pos(), 30, sounds::sound_t::combat, _( "VRRRRMP!" ), false, "bionic",
static_cast<std::string>( bio_resonator ) );
for( const tripoint &bashpoint : here.points_in_radius( pos(), 1 ) ) {
here.bash( bashpoint, 110 );
// Multibash effect, so that doors &c will fall
here.bash( bashpoint, 110 );
here.bash( bashpoint, 110 );
}
mod_moves( -100 );
} else if( bio.id == bio_time_freeze ) {
if( mounted ) {
refund_power();
add_msg_if_player( m_info, _( "You cannot activate %s while mounted." ), bio.info().name );
return false;
}
add_msg_activate();
mod_moves( units::to_kilojoule( get_power_level() ) );
set_power_level( 0_kJ );
add_msg_if_player( m_good, _( "Your speed suddenly increases!" ) );
if( one_in( 3 ) ) {
add_msg_if_player( m_bad, _( "Your muscles tear with the strain." ) );
apply_damage( nullptr, bodypart_id( "arm_l" ), rng( 5, 10 ) );
apply_damage( nullptr, bodypart_id( "arm_r" ), rng( 5, 10 ) );
apply_damage( nullptr, bodypart_id( "leg_l" ), rng( 7, 12 ) );
apply_damage( nullptr, bodypart_id( "leg_r" ), rng( 7, 12 ) );
apply_damage( nullptr, bodypart_id( "torso" ), rng( 5, 15 ) );
}
if( one_in( 5 ) ) {
add_effect( effect_teleglow, rng( 5_minutes, 40_minutes ) );
}
} else if( bio.id == bio_teleport ) {
if( mounted ) {
refund_power();
add_msg_if_player( m_info, _( "You cannot activate %s while mounted." ), bio.info().name );
return false;
}
add_msg_activate();
teleport::teleport( *this );
add_effect( effect_teleglow, 30_minutes );
mod_moves( -100 );
} else if( bio.id == bio_blood_anal ) {
add_msg_activate();
conduct_blood_analysis();
} else if( bio.id == bio_blood_filter ) {
add_msg_activate();
static const std::vector<efftype_id> removable = {{
effect_fungus, effect_dermatik, effect_bloodworms,
effect_poison, effect_stung, effect_badpoison,
effect_pkill1, effect_pkill2, effect_pkill3, effect_pkill_l,
effect_drunk, effect_cig, effect_cocaine_high, effect_weed_high,
effect_hallu, effect_visuals, effect_pblue, effect_iodine, effect_datura,
effect_took_xanax, effect_took_prozac, effect_took_prozac_bad,
effect_took_flumed,
}
};
for( const auto &eff : removable ) {
remove_effect( eff );
}
// Purging the substance won't remove the fatigue it caused
force_comedown( get_effect( effect_adrenaline ) );
force_comedown( get_effect( effect_meth ) );
set_painkiller( 0 );
set_stim( 0 );
mod_moves( -100 );
} else if( bio.id == bio_torsionratchet ) {
add_msg_activate();
add_msg_if_player( m_info, _( "Your torsion ratchet locks onto your joints." ) );
} else if( bio.id == bio_jointservo ) {
add_msg_activate();
add_msg_if_player( m_info, _( "You can now run faster, assisted by joint servomotors." ) );
} else if( bio.id == bio_lighter ) {
const std::optional<tripoint> pnt = choose_adjacent( _( "Start a fire where?" ) );
if( pnt && here.is_flammable( *pnt ) ) {
add_msg_activate();
here.add_field( *pnt, fd_fire, 1 );
mod_moves( -100 );
} else {
refund_power();
add_msg_if_player( m_info, _( "There's nothing to light there." ) );
return false;
}
} else if( bio.id == bio_geiger ) {
add_msg_activate();
add_msg_if_player( m_info, _( "Your radiation level: %d" ), get_rad() );
} else if( bio.id == bio_adrenaline ) {
add_msg_activate();
if( has_effect( effect_adrenaline ) ) {
add_msg_if_player( m_bad, _( "Safeguards kick in, and the bionic refuses to activate!" ) );
refund_power();
return false;
} else {
add_msg_activate();
add_effect( effect_adrenaline, 20_minutes );
}
} else if( bio.id == bio_emp ) {
if( const std::optional<tripoint> pnt = choose_adjacent( _( "Create an EMP where?" ) ) ) {
add_msg_activate();
explosion_handler::emp_blast( *pnt );
mod_moves( -100 );
} else {
refund_power();
return false;
}
} else if( bio.id == bio_hydraulics ) {
add_msg_activate();
add_msg_if_player( m_good, _( "Your muscles hiss as hydraulic strength fills them!" ) );
//~ Sound of hissing hydraulic muscle! (not quite as loud as a car horn)
sounds::sound( pos(), 19, sounds::sound_t::activity, _( "HISISSS!" ), false, "bionic",
static_cast<std::string>( bio_hydraulics ) );
} else if( bio.id == bio_water_extractor ) {
bool no_target = true;
bool extracted = false;
for( item &it : here.i_at( pos() ) ) {
static const auto volume_per_water_charge = 500_ml;
if( it.is_corpse() ) {
const int avail = it.get_var( "remaining_water", it.volume() / volume_per_water_charge );
if( avail > 0 ) {
no_target = false;
if( query_yn( _( "Extract water from the %s" ),
colorize( it.tname(), it.color_in_inventory() ) ) ) {
item water( itype_water_clean, calendar::turn, avail );
if( liquid_handler::consume_liquid( water ) ) {
add_msg_activate();
extracted = true;
it.set_var( "remaining_water", static_cast<int>( water.charges ) );
}
break;
}
}
}
}
if( no_target ) {
add_msg_if_player( m_bad, _( "There is no suitable corpse on this tile." ) );
}
if( !extracted ) {
refund_power();
return false;
}
} else if( bio.id == bio_magnet ) {
add_msg_activate();
static const std::set<material_id> affected_materials =
{ material_id( "iron" ), material_id( "steel" ) };
// Remember all items that will be affected, then affect them
// Don't "snowball" by affecting some items multiple times
std::vector<std::pair<item, tripoint>> affected;
const units::mass weight_cap = weight_capacity();
for( const tripoint &p : here.points_in_radius( pos(), 10 ) ) {
if( p == pos() || !here.has_items( p ) || here.has_flag( flag_SEALED, p ) ) {
continue;
}
map_stack stack = here.i_at( p );
for( auto it = stack.begin(); it != stack.end(); it++ ) {
if( it->weight() < weight_cap &&
it->made_of_any( affected_materials ) ) {
affected.emplace_back( std::make_pair( *it, p ) );
stack.erase( it );
break;
}
}
}
for( const std::pair<item, tripoint> &pr : affected ) {
projectile proj;
proj.speed = 50;
proj.impact = damage_instance::physical( pr.first.weight() / 250_gram, 0, 0, 0 );
// make the projectile stop one tile short to prevent hitting the player
proj.range = rl_dist( pr.second, pos() ) - 1;
static const std::set<ammo_effect_str_id> ammo_effects = {{
ammo_effect_str_id( "NO_ITEM_DAMAGE" ),
ammo_effect_str_id( "DRAW_AS_LINE" ),
ammo_effect_str_id( "NO_DAMAGE_SCALING" ),
ammo_effect_str_id( "JET" ),
}
};
for( const auto &eff : ammo_effects ) {
proj.add_effect( eff );
}
dealt_projectile_attack dealt = projectile_attack(
proj, pr.second, pos(), dispersion_sources{ 0 }, this );
here.add_item_or_charges( dealt.end_point, pr.first );
}
mod_moves( -100 );
} else if( bio.id == bio_lockpick ) {
bool used = false;
bool tried_lockpick = false;
const std::optional<tripoint> pnt = choose_adjacent( _( "Use your lockpick where?" ) );
std::string open_message;
if( pnt ) {
tried_lockpick = true;
ter_id ter_type = g->m.ter( *pnt );
furn_id furn_type = g->m.furn( *pnt );
lockpicking_open_result lr = get_lockpicking_open_result( ter_type, furn_type );
ter_id new_ter_type = lr.new_ter_type;
furn_id new_furn_type = lr.new_furn_type;
open_message = lr.open_message;
if( new_ter_type != t_null || new_furn_type != f_null ) {
g->m.has_furn( *pnt ) ?
g->m.furn_set( *pnt, new_furn_type ) :
static_cast<void>( g->m.ter_set( *pnt, new_ter_type ) );
used = true;
}
}
if( used ) {
add_msg_activate();
add_msg_if_player( m_good, open_message );
mod_moves( -100 );
} else {
refund_power();
if( tried_lockpick ) {
add_msg_if_player( m_info, _( "There is nothing to lockpick nearby." ) );
}
return false;
}
} else if( bio.id == bio_flashbang ) {
add_msg_activate();
explosion_handler::flashbang( pos(), true, "explosion" );
mod_moves( -100 );
} else if( bio.id == bio_shockwave ) {
add_msg_activate();
shockwave_data sw;
sw.affects_player = false;
sw.radius = 3;
sw.force = 4;
sw.stun = 2;
sw.dam_mult = 8;
// affects_player is always false, so assuming the player is always the source of this
explosion_handler::shockwave( pos(), sw, "explosion", &get_player_character() );
add_msg_if_player( m_neutral, _( "You unleash a powerful shockwave!" ) );
mod_moves( -100 );
} else if( bio.id == bio_meteorologist ) {
const weather_manager &weather = get_weather();
add_msg_activate();
// Calculate local wind power
int vehwindspeed = 0;
if( optional_vpart_position vp = here.veh_at( pos() ) ) {
// vehicle velocity in mph
vehwindspeed = std::abs( vp->vehicle().velocity / 100 );
}
const oter_id &cur_om_ter = overmap_buffer.ter( global_omt_location() );
/* cache g->get_temperature( player location ) since it is used twice. No reason to recalc */
const auto player_local_temp = weather.get_temperature( g->u.pos() );
/* windpower defined in internal velocity units (=.01 mph) */
double windpower = 100.0f * get_local_windpower( weather.windspeed + vehwindspeed,
cur_om_ter, pos(), weather.winddirection, g->is_sheltered( pos() ) );
add_msg_if_player( m_info, _( "Temperature: %s." ), print_temperature( player_local_temp ) );
add_msg_if_player( m_info, _( "Relative Humidity: %s." ),
print_humidity(
get_local_humidity( weatherPoint.humidity, weather.weather_id,
g->is_sheltered( g->u.pos() ) ) ) );
add_msg_if_player( m_info, _( "Pressure: %s." ),
print_pressure( static_cast<int>( weatherPoint.pressure ) ) );
add_msg_if_player( m_info, _( "Wind Speed: %.1f %s." ),
convert_velocity( static_cast<int>( windpower ), VU_WIND ),
velocity_units( VU_WIND ) );
add_msg_if_player( m_info, _( "Feels Like: %s." ),
print_temperature(
get_local_windchill( units::to_fahrenheit( weatherPoint.temperature ),
weatherPoint.humidity,
windpower / 100 ) + player_local_temp ) );
std::string dirstring = get_dirstring( weather.winddirection );
add_msg_if_player( m_info, _( "Wind Direction: From the %s." ), dirstring );
} else if( bio.id == bio_remote ) {
add_msg_activate();