forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iuse_actor.cpp
5080 lines (4445 loc) · 179 KB
/
iuse_actor.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 "iuse_actor.h"
#include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstddef>
#include <functional>
#include <iterator>
#include <list>
#include <memory>
#include <sstream>
#include "action.h"
#include "activity_handlers.h"
#include "ammo.h"
#include "animation.h"
#include "assign.h"
#include "avatar.h"
#include "avatar_functions.h"
#include "bionics.h"
#include "bodypart.h"
#include "calendar.h"
#include "cata_utility.h"
#include "character.h"
#include "character_functions.h"
#include "character_id.h"
#include "clothing_mod.h"
#include "crafting.h"
#include "creature.h"
#include "debug.h"
#include "effect.h"
#include "enum_conversions.h"
#include "enums.h"
#include "explosion.h"
#include "field_type.h"
#include "flat_set.h"
#include "game.h"
#include "game_inventory.h"
#include "int_id.h"
#include "inventory.h"
#include "item.h"
#include "item_contents.h"
#include "item_factory.h"
#include "item_group.h"
#include "item_location.h"
#include "itype.h"
#include "json.h"
#include "line.h"
#include "magic.h"
#include "map.h"
#include "map_iterator.h"
#include "map_selector.h"
#include "mapdata.h"
#include "material.h"
#include "memory_fast.h"
#include "messages.h"
#include "monster.h"
#include "morale_types.h"
#include "mtype.h"
#include "mutation.h"
#include "options.h"
#include "output.h"
#include "overmapbuffer.h"
#include "player.h"
#include "player_activity.h"
#include "pldata.h"
#include "point.h"
#include "recipe.h"
#include "recipe_dictionary.h"
#include "requirements.h"
#include "rng.h"
#include "skill.h"
#include "sounds.h"
#include "string_formatter.h"
#include "string_input_popup.h"
#include "translations.h"
#include "trap.h"
#include "ui.h"
#include "units_utility.h"
#include "value_ptr.h"
#include "vehicle.h"
#include "vehicle_selector.h"
#include "visitable.h"
#include "vitamin.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "weather.h"
static const activity_id ACT_FIRSTAID( "ACT_FIRSTAID" );
static const activity_id ACT_MAKE_ZLAVE( "ACT_MAKE_ZLAVE" );
static const activity_id ACT_RELOAD( "ACT_RELOAD" );
static const activity_id ACT_REPAIR_ITEM( "ACT_REPAIR_ITEM" );
static const activity_id ACT_SPELLCASTING( "ACT_SPELLCASTING" );
static const activity_id ACT_STUDY_SPELL( "ACT_STUDY_SPELL" );
static const activity_id ACT_START_FIRE( "ACT_START_FIRE" );
static const efftype_id effect_accumulated_mutagen( "accumulated_mutagen" );
static const efftype_id effect_asthma( "asthma" );
static const efftype_id effect_bandaged( "bandaged" );
static const efftype_id effect_bite( "bite" );
static const efftype_id effect_bleed( "bleed" );
static const efftype_id effect_pet( "pet" );
static const efftype_id effect_disinfected( "disinfected" );
static const efftype_id effect_downed( "downed" );
static const efftype_id effect_infected( "infected" );
static const efftype_id effect_music( "music" );
static const efftype_id effect_playing_instrument( "playing_instrument" );
static const efftype_id effect_recover( "recover" );
static const efftype_id effect_sleep( "sleep" );
static const efftype_id effect_stunned( "stunned" );
static const fault_id fault_bionic_nonsterile( "fault_bionic_nonsterile" );
static const bionic_id bio_syringe( "bio_syringe" );
static const itype_id itype_barrel_small( "barrel_small" );
static const itype_id itype_brazier( "brazier" );
static const itype_id itype_char_smoker( "char_smoker" );
static const itype_id itype_fire( "fire" );
static const itype_id itype_stock_small( "stock_small" );
static const itype_id itype_syringe( "syringe" );
static const skill_id skill_fabrication( "fabrication" );
static const skill_id skill_firstaid( "firstaid" );
static const skill_id skill_survival( "survival" );
static const species_id HUMAN( "HUMAN" );
static const species_id ZOMBIE( "ZOMBIE" );
static const trait_id trait_CENOBITE( "CENOBITE" );
static const trait_id trait_DEBUG_BIONICS( "DEBUG_BIONICS" );
static const trait_id trait_TOLERANCE( "TOLERANCE" );
static const trait_id trait_INFRESIST( "INFRESIST" );
static const trait_id trait_LIGHTWEIGHT( "LIGHTWEIGHT" );
static const trait_id trait_PACIFIST( "PACIFIST" );
static const trait_id trait_PSYCHOPATH( "PSYCHOPATH" );
static const trait_id trait_PYROMANIA( "PYROMANIA" );
static const trait_id trait_NOPAIN( "NOPAIN" );
static const trait_id trait_MASOCHIST( "MASOCHIST" );
static const trait_id trait_MASOCHIST_MED( "MASOCHIST_MED" );
static const trait_id trait_MUT_JUNKIE( "MUT_JUNKIE" );
static const trait_id trait_SAPIOVORE( "SAPIOVORE" );
static const trait_id trait_SELFAWARE( "SELFAWARE" );
static const quality_id qual_LOCKPICK( "LOCKPICK" );
static const std::string flag_FIT( "FIT" );
static const std::string flag_OVERSIZE( "OVERSIZE" );
static const std::string flag_UNDERSIZE( "UNDERSIZE" );
static const std::string flag_VARSIZE( "VARSIZE" );
static const std::string flag_POWERARMOR_MOD( "POWERARMOR_MOD" );
class npc;
std::unique_ptr<iuse_actor> iuse_transform::clone() const
{
return std::make_unique<iuse_transform>( *this );
}
void iuse_transform::load( const JsonObject &obj )
{
obj.read( "target", target, true );
obj.read( "msg", msg_transform );
obj.read( "container", container );
if( obj.has_member( "target_charges" ) && obj.has_member( "rand_target_charges" ) ) {
obj.throw_error( "Transform actor specified both fixed and random target charges",
"target_charges" );
}
obj.read( "target_charges", ammo_qty );
if( obj.has_array( "rand_target_charges" ) ) {
for( const int charge : obj.get_array( "rand_target_charges" ) ) {
random_ammo_qty.push_back( charge );
}
if( random_ammo_qty.size() < 2 ) {
obj.throw_error( "You must specify two or more values to choose between", "rand_target_charges" );
}
}
obj.read( "target_ammo", ammo_type );
obj.read( "countdown", countdown );
if( !ammo_type.is_empty() && !container.is_empty() ) {
obj.throw_error( "Transform actor specified both ammo type and container type", "target_ammo" );
}
obj.read( "active", active );
obj.read( "moves", moves );
if( moves < 0 ) {
obj.throw_error( "transform actor specified negative moves", "moves" );
}
obj.read( "need_fire", need_fire );
need_fire = std::max( need_fire, 0 );
if( !obj.read( "need_charges_msg", need_charges_msg ) ) {
need_charges_msg = to_translation( "The %s is empty!" );
}
obj.read( "need_charges", need_charges );
need_charges = std::max( need_charges, 0 );
if( !obj.read( "need_fire_msg", need_fire_msg ) ) {
need_fire_msg = to_translation( "You need a source of fire!" );
}
obj.read( "need_worn", need_worn );
obj.read( "need_wielding", need_wielding );
obj.read( "qualities_needed", qualities_needed );
obj.read( "menu_text", menu_text );
}
int iuse_transform::use( player &p, item &it, bool t, const tripoint &pos ) const
{
if( t ) {
return 0; // invoked from active item processing, do nothing.
}
const bool possess = p.has_item( it ) ||
( it.has_flag( "ALLOWS_REMOTE_USE" ) && square_dist( p.pos(), pos ) == 1 );
if( possess && need_worn && !p.is_worn( it ) ) {
p.add_msg_if_player( m_info, _( "You need to wear the %1$s before activating it." ), it.tname() );
return 0;
}
if( possess && need_wielding && !p.is_wielding( it ) ) {
p.add_msg_if_player( m_info, _( "You need to wield the %1$s before activating it." ), it.tname() );
return 0;
}
if( need_charges ) {
if( it.has_flag( flag_POWERARMOR_MOD ) && character_funcs::can_interface_armor( p ) ) {
if( !p.has_power() ) {
if( possess ) {
p.add_msg_if_player( m_info, need_charges_msg, it.tname() );
}
return 0;
}
} else if( it.units_remaining( p ) < need_charges ) {
if( possess ) {
p.add_msg_if_player( m_info, need_charges_msg, it.tname() );
}
return 0;
}
}
if( need_fire && possess ) {
if( !p.use_charges_if_avail( itype_fire, need_fire ) ) {
p.add_msg_if_player( m_info, need_fire_msg, it.tname() );
return 0;
}
if( p.is_underwater() ) {
p.add_msg_if_player( m_info, _( "You can't do that while underwater" ) );
return 0;
}
}
if( possess && !msg_transform.empty() ) {
p.add_msg_if_player( m_neutral, msg_transform, it.tname() );
}
if( possess ) {
p.moves -= moves;
}
item obj_copy( it );
item *obj;
// defined here to allow making a new item assigned to the pointer
item obj_it;
if( container.is_empty() ) {
obj = &it.convert( target );
if( ammo_qty >= 0 || !random_ammo_qty.empty() ) {
int qty;
if( !random_ammo_qty.empty() ) {
const auto index = rng( 1, random_ammo_qty.size() - 1 );
qty = rng( random_ammo_qty[index - 1], random_ammo_qty[index] );
} else {
qty = ammo_qty;
}
if( !ammo_type.is_empty() ) {
obj->ammo_set( ammo_type, qty );
} else if( !obj->ammo_current().is_null() ) {
obj->ammo_set( obj->ammo_current(), qty );
} else {
obj->set_countdown( qty );
}
}
} else {
it.convert( container );
obj_it = item( target, calendar::turn, std::max( ammo_qty, 1 ) );
obj = &obj_it;
it.put_in( *obj );
}
if( p.is_worn( *obj ) ) {
p.reset_encumbrance();
// This is most likely wrong: it doubles temperature shift for the turn!
p.update_bodytemp( get_map(), get_weather() );
p.on_worn_item_transform( obj_copy, *obj );
}
obj->item_counter = countdown > 0 ? countdown : obj->type->countdown_interval;
obj->active = active || obj->item_counter;
return 0;
}
ret_val<bool> iuse_transform::can_use( const Character &p, const item &, bool,
const tripoint & ) const
{
if( qualities_needed.empty() ) {
return ret_val<bool>::make_success();
}
std::map<quality_id, int> unmet_reqs;
inventory inv;
inv.form_from_map( p.pos(), 1, &p, true, true );
for( const auto &quality : qualities_needed ) {
if( !p.has_quality( quality.first, quality.second ) &&
!inv.has_quality( quality.first, quality.second ) ) {
unmet_reqs.insert( quality );
}
}
if( unmet_reqs.empty() ) {
return ret_val<bool>::make_success();
}
std::string unmet_reqs_string = enumerate_as_string( unmet_reqs.begin(), unmet_reqs.end(),
[&]( const std::pair<quality_id, int> &unmet_req ) {
return string_format( "%s %d", unmet_req.first.obj().name, unmet_req.second );
} );
return ret_val<bool>::make_failure( vgettext( "You need a tool with %s.", "You need tools with %s.",
unmet_reqs.size() ), unmet_reqs_string );
}
std::string iuse_transform::get_name() const
{
if( !menu_text.empty() ) {
return menu_text.translated();
}
return iuse_actor::get_name();
}
void iuse_transform::finalize( const itype_id & )
{
if( !target.is_valid() ) {
debugmsg( "Invalid transform target: %s", target.c_str() );
}
if( !container.is_empty() ) {
if( !container.is_valid() ) {
debugmsg( "Invalid transform container: %s", container.c_str() );
}
item dummy( target );
if( ammo_qty > 1 && !dummy.count_by_charges() ) {
debugmsg( "Transform target with container must be an item with charges, got non-charged: %s",
target.c_str() );
}
}
}
void iuse_transform::info( const item &it, std::vector<iteminfo> &dump ) const
{
item dummy( target, calendar::turn, std::max( ammo_qty, 1 ) );
if( it.has_flag( "FIT" ) ) {
dummy.set_flag( "FIT" );
}
dump.emplace_back( "TOOL", string_format( _( "<bold>Turns into</bold>: %s" ),
dummy.tname() ) );
if( countdown > 0 ) {
dump.emplace_back( "TOOL", _( "Countdown: " ), countdown );
}
const auto *explosion_use = dummy.get_use( "explosion" );
if( explosion_use != nullptr ) {
explosion_use->get_actor_ptr()->info( it, dump );
}
}
std::unique_ptr<iuse_actor> unpack_actor::clone() const
{
return std::make_unique<unpack_actor>( *this );
}
void unpack_actor::load( const JsonObject &obj )
{
obj.read( "group", unpack_group );
obj.read( "items_fit", items_fit );
assign( obj, "filthy_volume_threshold", filthy_vol_threshold );
}
int unpack_actor::use( player &p, item &it, bool, const tripoint & ) const
{
std::vector<item> items = item_group::items_from( unpack_group, calendar::turn );
item last_armor;
p.add_msg_if_player( _( "You unpack the %s." ), it.tname() );
map &here = get_map();
for( item &content : items ) {
if( content.is_armor() ) {
if( items_fit ) {
content.set_flag( "FIT" );
} else if( content.typeId() == last_armor.typeId() ) {
if( last_armor.has_flag( "FIT" ) ) {
content.set_flag( "FIT" );
} else if( !last_armor.has_flag( "FIT" ) ) {
content.unset_flag( "FIT" );
}
}
last_armor = content;
}
if( content.get_storage() >= filthy_vol_threshold && it.has_flag( "FILTHY" ) ) {
content.set_flag( "FILTHY" );
}
here.add_item_or_charges( p.pos(), content );
}
p.i_rem( &it );
return 0;
}
void unpack_actor::info( const item &, std::vector<iteminfo> &dump ) const
{
dump.emplace_back( "DESCRIPTION",
_( "This item could be unpacked to receive something." ) );
}
std::unique_ptr<iuse_actor> countdown_actor::clone() const
{
return std::make_unique<countdown_actor>( *this );
}
void countdown_actor::load( const JsonObject &obj )
{
obj.read( "name", name );
obj.read( "interval", interval );
obj.read( "message", message );
}
int countdown_actor::use( player &p, item &it, bool t, const tripoint &pos ) const
{
if( t ) {
return 0;
}
if( it.active ) {
return 0;
}
if( p.sees( pos ) && !message.empty() ) {
p.add_msg_if_player( m_neutral, _( message ), it.tname() );
}
it.item_counter = interval > 0 ? interval : it.type->countdown_interval;
it.active = true;
return 0;
}
ret_val<bool> countdown_actor::can_use( const Character &, const item &it, bool,
const tripoint & ) const
{
if( it.active ) {
return ret_val<bool>::make_failure( _( "It's already been triggered." ) );
}
return ret_val<bool>::make_success();
}
std::string countdown_actor::get_name() const
{
if( !name.empty() ) {
return name;
}
return iuse_actor::get_name();
}
void countdown_actor::info( const item &it, std::vector<iteminfo> &dump ) const
{
dump.emplace_back( "TOOL", _( "Countdown: " ),
interval > 0 ? interval : it.type->countdown_interval );
const auto countdown_actor = it.type->countdown_action.get_actor_ptr();
if( countdown_actor != nullptr ) {
countdown_actor->info( it, dump );
}
}
std::unique_ptr<iuse_actor> explosion_iuse::clone() const
{
return std::make_unique<explosion_iuse>( *this );
}
// For an explosion (which releases some kind of gas), this function
// calculates the points around that explosion where to create those
// gas fields.
// Those points must have a clear line of sight and a clear path to
// the center of the explosion.
// They must also be passable.
static std::vector<tripoint> points_for_gas_cloud( const tripoint ¢er, int radius )
{
map &here = get_map();
std::vector<tripoint> result;
for( const auto &p : closest_points_first( center, radius ) ) {
if( here.impassable( p ) ) {
continue;
}
if( p != center ) {
if( !here.clear_path( center, p, radius, 1, 100 ) ) {
// Can not splatter gas from center to that point, something is in the way
continue;
}
}
result.push_back( p );
}
return result;
}
void explosion_iuse::load( const JsonObject &obj )
{
if( obj.has_object( "explosion" ) ) {
auto expl = obj.get_object( "explosion" );
explosion = load_explosion_data( expl );
}
obj.read( "draw_explosion_radius", draw_explosion_radius );
if( obj.has_member( "draw_explosion_color" ) ) {
draw_explosion_color = color_from_string( obj.get_string( "draw_explosion_color" ) );
}
obj.read( "do_flashbang", do_flashbang );
obj.read( "flashbang_player_immune", flashbang_player_immune );
obj.read( "fields_radius", fields_radius );
if( obj.has_member( "fields_type" ) || fields_radius > 0 ) {
fields_type = field_type_id( obj.get_string( "fields_type" ) );
}
obj.read( "fields_min_intensity", fields_min_intensity );
obj.read( "fields_max_intensity", fields_max_intensity );
if( fields_max_intensity == 0 ) {
fields_max_intensity = fields_type.obj().get_max_intensity();
}
obj.read( "emp_blast_radius", emp_blast_radius );
obj.read( "scrambler_blast_radius", scrambler_blast_radius );
obj.read( "sound_volume", sound_volume );
obj.read( "sound_msg", sound_msg );
obj.read( "no_deactivate_msg", no_deactivate_msg );
}
int explosion_iuse::use( player &p, item &it, bool t, const tripoint &pos ) const
{
if( t ) {
if( sound_volume >= 0 ) {
sounds::sound( pos, sound_volume, sounds::sound_t::alarm,
sound_msg.empty() ? _( "Tick." ) : _( sound_msg ), true, "misc", "bomb_ticking" );
}
return 0;
}
if( it.charges > 0 ) {
if( p.has_item( it ) ) {
if( no_deactivate_msg.empty() ) {
p.add_msg_if_player( m_warning,
_( "You've already set the %s's timer you might want to get away from it." ), it.tname() );
} else {
p.add_msg_if_player( m_info, _( no_deactivate_msg ), it.tname() );
}
}
return 0;
}
trigger_explosion( pos, it.activated_by.get() );
return 1;
}
void explosion_iuse::trigger_explosion( const tripoint &pos, Creature *source ) const
{
if( explosion ) {
explosion_handler::explosion( pos, explosion, source );
}
if( draw_explosion_radius >= 0 ) {
explosion_handler::draw_explosion( pos, draw_explosion_radius, draw_explosion_color, "explosion" );
}
if( do_flashbang ) {
explosion_handler::flashbang( pos, flashbang_player_immune, "explosion" );
}
map &here = get_map();
if( fields_radius >= 0 && fields_type.id() ) {
std::vector<tripoint> gas_sources = points_for_gas_cloud( pos, fields_radius );
for( auto &gas_source : gas_sources ) {
const int field_intensity = rng( fields_min_intensity, fields_max_intensity );
here.add_field( gas_source, fields_type, field_intensity, 1_turns );
}
}
if( scrambler_blast_radius >= 0 ) {
for( const tripoint &dest : here.points_in_radius( pos, scrambler_blast_radius ) ) {
explosion_handler::scrambler_blast( dest );
}
}
if( emp_blast_radius >= 0 ) {
for( const tripoint &dest : here.points_in_radius( pos, emp_blast_radius ) ) {
explosion_handler::emp_blast( dest );
}
}
}
void explosion_iuse::info( const item &, std::vector<iteminfo> &dump ) const
{
if( explosion.damage > 0 ) {
dump.emplace_back( "TOOL", _( "Blast damage at epicenter: " ),
explosion.damage );
dump.emplace_back( "TOOL", _( "Blast radius: " ), static_cast<int>( explosion.radius ) );
}
const auto &sd = explosion.fragment;
if( sd ) {
dump.emplace_back( "TOOL", _( "Shrapnel damage: " ),
static_cast<int>( sd->impact.total_damage() ) );
dump.emplace_back( "TOOL", _( "Shrapnel range: " ), sd->range );
}
// TODO: List other effects, like EMP and clouds
}
std::unique_ptr<iuse_actor> unfold_vehicle_iuse::clone() const
{
return std::make_unique<unfold_vehicle_iuse>( *this );
}
void unfold_vehicle_iuse::load( const JsonObject &obj )
{
vehicle_id = vproto_id( obj.get_string( "vehicle_name" ) );
obj.read( "unfold_msg", unfold_msg );
obj.read( "moves", moves );
obj.read( "tools_needed", tools_needed );
}
int unfold_vehicle_iuse::use( player &p, item &it, bool, const tripoint & ) const
{
if( p.is_underwater() ) {
p.add_msg_if_player( m_info, _( "You can't do that while underwater." ) );
return 0;
}
if( p.is_mounted() ) {
p.add_msg_if_player( m_info, _( "You cannot do that while mounted." ) );
return 0;
}
for( const auto &tool : tools_needed ) {
// Amount == -1 means need one, but don't consume it.
if( !p.has_amount( tool.first, 1 ) ) {
p.add_msg_if_player( _( "You need %s to do it!" ),
item::nname( tool.first ) );
return 0;
}
}
vehicle *veh = get_map().add_vehicle( vehicle_id, p.pos(), 0_degrees, 0, 0, false );
if( veh == nullptr ) {
p.add_msg_if_player( m_info, _( "There's no room to unfold the %s." ), it.tname() );
return 0;
}
veh->set_owner( p );
// Mark the vehicle as foldable.
veh->tags.insert( "convertible" );
// Store the id of the item the vehicle is made of.
veh->tags.insert( std::string( "convertible:" ) + it.typeId().str() );
if( !unfold_msg.empty() ) {
p.add_msg_if_player( _( unfold_msg ), it.tname() );
}
p.moves -= moves;
// Restore HP of parts if we stashed them previously.
if( it.has_var( "folding_bicycle_parts" ) ) {
// Brand new, no HP stored
return 1;
}
std::istringstream veh_data;
const auto data = it.get_var( "folding_bicycle_parts" );
veh_data.str( data );
if( !data.empty() && data[0] >= '0' && data[0] <= '9' ) {
// starts with a digit -> old format
for( const vpart_reference &vpr : veh->get_all_parts() ) {
int tmp;
veh_data >> tmp;
veh->set_hp( vpr.part(), tmp );
}
} else {
try {
JsonIn json( veh_data );
// Load parts into a temporary vector to not override
// cached values (like precalc, passenger_id, ...)
std::vector<vehicle_part> parts;
json.read( parts );
for( size_t i = 0; i < parts.size() && i < static_cast<size_t>( veh->part_count() ); i++ ) {
const vehicle_part &src = parts[i];
vehicle_part &dst = veh->part( i );
// and now only copy values, that are
// expected to be consistent.
veh->set_hp( dst, src.hp() );
dst.blood = src.blood;
// door state/amount of fuel/direction of headlight
dst.ammo_set( src.ammo_current(), src.ammo_remaining() );
dst.flags = src.flags;
}
} catch( const JsonError &e ) {
debugmsg( "Error restoring vehicle: %s", e.c_str() );
}
}
return 1;
}
std::unique_ptr<iuse_actor> consume_drug_iuse::clone() const
{
return std::make_unique<consume_drug_iuse>( *this );
}
static effect_data load_effect_data( const JsonObject &e )
{
time_duration time;
if( e.has_string( "duration" ) ) {
time = read_from_json_string<time_duration>( *e.get_raw( "duration" ), time_duration::units );
} else {
time = time_duration::from_turns( e.get_int( "duration", 0 ) );
}
if( e.get_bool( "permanent", false ) ) {
effect_data ret( efftype_id( e.get_string( "id" ) ), time,
get_body_part_token( e.get_string( "bp", "NUM_BP" ) ) );
ret.permanent = true;
if( json_report_strict ) {
try {
e.throw_error( "Effect permanence has been moved to effect_type. Set permanence there.",
"permanent" );
} catch( const JsonError &ex ) {
debugmsg( "\n%s", ex.what() );
}
}
return ret;
} else {
return effect_data( efftype_id( e.get_string( "id" ) ), time,
get_body_part_token( e.get_string( "bp", "NUM_BP" ) ) );
}
}
void consume_drug_iuse::load( const JsonObject &obj )
{
obj.read( "activation_message", activation_message );
obj.read( "charges_needed", charges_needed );
obj.read( "tools_needed", tools_needed );
if( obj.has_array( "effects" ) ) {
for( const JsonObject e : obj.get_array( "effects" ) ) {
effects.push_back( load_effect_data( e ) );
}
}
obj.read( "stat_adjustments", stat_adjustments );
obj.read( "fields_produced", fields_produced );
obj.read( "moves", moves );
for( JsonArray vit : obj.get_array( "vitamins" ) ) {
auto lo = vit.get_int( 1 );
auto hi = vit.size() >= 3 ? vit.get_int( 2 ) : lo;
vitamins.emplace( vitamin_id( vit.get_string( 0 ) ), std::make_pair( lo, hi ) );
}
used_up_item = obj.get_string( "used_up_item", used_up_item );
}
void consume_drug_iuse::info( const item &, std::vector<iteminfo> &dump ) const
{
const std::string vits = enumerate_as_string( vitamins.begin(), vitamins.end(),
[]( const decltype( vitamins )::value_type & v ) {
const time_duration rate = get_player_character().vitamin_rate( v.first );
if( rate <= 0_turns ) {
return std::string();
}
const int lo = static_cast<int>( v.second.first * rate / 1_days * 100 );
const int hi = static_cast<int>( v.second.second * rate / 1_days * 100 );
return string_format( lo == hi ? "%s (%i%%)" : "%s (%i-%i%%)", v.first.obj().name(), lo,
hi );
} );
if( !vits.empty() ) {
dump.emplace_back( "TOOL", _( "Vitamins (RDA): " ), vits );
}
if( tools_needed.count( itype_syringe ) ) {
dump.emplace_back( "TOOL", _( "You need a <info>syringe</info> to inject this drug." ) );
}
}
int consume_drug_iuse::use( player &p, item &it, bool, const tripoint & ) const
{
auto need_these = tools_needed;
if( need_these.count( itype_syringe ) && p.has_bionic( bio_syringe ) ) {
need_these.erase( itype_syringe ); // no need for a syringe with bionics like these!
}
// Check prerequisites first.
for( const auto &tool : need_these ) {
// Amount == -1 means need one, but don't consume it.
if( !p.has_amount( tool.first, 1 ) ) {
p.add_msg_player_or_say( _( "You need %1$s to consume %2$s!" ),
_( "I need a %1$s to consume %2$s!" ),
item::nname( tool.first ),
it.type_name( 1 ) );
return -1;
}
}
for( const auto &consumable : charges_needed ) {
// Amount == -1 means need one, but don't consume it.
if( !p.has_charges( consumable.first, ( consumable.second == -1 ) ?
1 : consumable.second ) ) {
p.add_msg_player_or_say( _( "You need %1$s to consume %2$s!" ),
_( "I need a %1$s to consume %2$s!" ),
item::nname( consumable.first ),
it.type_name( 1 ) );
return -1;
}
}
// Apply the various effects.
for( const auto &eff : effects ) {
time_duration dur = eff.duration;
if( p.has_trait( trait_TOLERANCE ) ) {
dur *= .8;
} else if( p.has_trait( trait_LIGHTWEIGHT ) ) {
dur *= 1.2;
}
p.add_effect( eff.id, dur, eff.bp );
if( eff.permanent ) {
p.get_effect( eff.id, eff.bp ).set_permanent();
}
}
for( const auto &stat_adjustment : stat_adjustments ) {
p.mod_stat( stat_adjustment.first, stat_adjustment.second );
}
map &here = get_map();
for( const auto &field : fields_produced ) {
const field_type_id fid = field_type_id( field.first );
for( int i = 0; i < 3; i++ ) {
here.add_field( {p.posx() + static_cast<int>( rng( -2, 2 ) ), p.posy() + static_cast<int>( rng( -2, 2 ) ), p.posz()},
fid,
field.second );
}
}
// for vitamins that accumulate (max > 0) multivitamins risk causing hypervitaminosis
for( const auto &v : vitamins ) {
// players with mutations that remove the requirement for a vitamin cannot suffer accumulation of it
p.vitamin_mod( v.first, rng( v.second.first, v.second.second ),
p.vitamin_rate( v.first ) <= 0_turns );
}
// Output message.
p.add_msg_if_player( _( activation_message ), it.type_name( 1 ) );
// Consume charges.
for( const auto &consumable : charges_needed ) {
if( consumable.second != -1 ) {
p.use_charges( consumable.first, consumable.second );
}
}
if( !used_up_item.empty() ) {
item used_up( used_up_item, it.birthday() );
p.i_add_or_drop( used_up );
}
p.moves -= moves;
return it.type->charges_to_use();
}
std::unique_ptr<iuse_actor> delayed_transform_iuse::clone() const
{
return std::make_unique<delayed_transform_iuse>( *this );
}
void delayed_transform_iuse::load( const JsonObject &obj )
{
iuse_transform::load( obj );
not_ready_msg = obj.get_string( "not_ready_msg" );
transform_age = obj.get_int( "transform_age" );
}
int delayed_transform_iuse::time_to_do( const item &it ) const
{
// TODO: change return type to time_duration
return transform_age - to_turns<int>( it.age() );
}
int delayed_transform_iuse::use( player &p, item &it, bool t, const tripoint &pos ) const
{
if( time_to_do( it ) > 0 ) {
p.add_msg_if_player( m_info, _( not_ready_msg ) );
return 0;
}
return iuse_transform::use( p, it, t, pos );
}
std::unique_ptr<iuse_actor> set_transform_iuse::clone() const
{
return std::make_unique<set_transform_iuse>( *this );
}
void set_transform_iuse::load( const JsonObject &obj )
{
iuse_transform::load( obj );
obj.read( "turn_off", turn_off );
obj.read( "flag", flag );
if( !obj.read( "set_charges_msg", set_charges_msg ) ) {
set_charges_msg = to_translation( "The %s is empty!" );
}
if( !obj.read( "set_charges", set_charges ) ) {
set_charges = 0;
}
set_charges = std::max( set_charges, 0 );
}
int set_transform_iuse::use( player &p, item &it, bool t, const tripoint &pos ) const
{
if( t ) {
return 0; // invoked from active item processing, do nothing.
}
const bool possess = p.has_item( it ) ||
( it.has_flag( "ALLOWS_REMOTE_USE" ) && square_dist( p.pos(), pos ) == 1 );
if( set_charges ) {
if( it.is_power_armor() && character_funcs::can_interface_armor( p ) ) {
if( !p.has_power() ) {
if( possess ) {
p.add_msg_if_player( m_info, set_charges_msg, it.tname() );
}
return 0;
}
} else if( it.units_remaining( p ) < set_charges ) {
if( possess ) {
p.add_msg_if_player( m_info, set_charges_msg, it.tname() );
}
return 0;
}
}
iuse_transform::use( p, it, t, pos );
for( auto &elem : p.worn ) {
if( elem.has_flag( flag ) && elem.active == turn_off ) {
if( elem.type->can_use( "set_transformed" ) ) {
const set_transformed_iuse *actor = dynamic_cast<const set_transformed_iuse *>
( elem.get_use( "set_transformed" )->get_actor_ptr() );
if( actor == nullptr ) {
debugmsg( "iuse_actor type descriptor and actual type mismatch" );
} else {
actor->bypass( p, elem, t, pos );
}
} else {
debugmsg( "Expected set_transformed function" );
}
}
}
return 0;
}
std::unique_ptr<iuse_actor> set_transformed_iuse::clone() const
{
return std::make_unique<set_transformed_iuse>( *this );
}
void set_transformed_iuse::load( const JsonObject &obj )
{
iuse_transform::load( obj );
obj.read( "restricted", restricted );
obj.read( "dependencies", dependencies );
}
int set_transformed_iuse::use( player &p, item &it, bool t, const tripoint &pos ) const
{
if( t ) {
return 0; // invoked from active item processing, do nothing.
}
iuse_transform::use( p, it, t, pos );
return 0;
}
int set_transformed_iuse::bypass( player &p, item &it, bool t, const tripoint &pos ) const
{
return iuse_transform::use( p, it, t, pos );
}
ret_val<bool> set_transformed_iuse::can_use( const Character &, const item &, bool,
const tripoint & ) const
{
if( restricted ) {
return ret_val<bool>::make_failure( _( "Activate via main piece." ) );
}
return ret_val<bool>::make_success();
}
std::unique_ptr<iuse_actor> place_monster_iuse::clone() const