-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
iuse_actor.cpp
4780 lines (4198 loc) · 168 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 <cctype>
#include <cstddef>
#include <algorithm>
#include <sstream>
#include <array>
#include <cmath>
#include <functional>
#include <iterator>
#include <list>
#include <memory>
#include "action.h"
#include "activity_handlers.h"
#include "avatar.h"
#include "ammo.h"
#include "assign.h"
#include "bionics.h"
#include "bodypart.h"
#include "calendar.h"
#include "cata_utility.h"
#include "character.h"
#include "crafting.h"
#include "creature.h"
#include "debug.h"
#include "vpart_position.h"
#include "effect.h"
#include "timed_event.h"
#include "explosion.h"
#include "field.h"
#include "game.h"
#include "game_inventory.h"
#include "item.h"
#include "item_factory.h"
#include "itype.h"
#include "magic.h"
#include "map.h"
#include "map_iterator.h"
#include "map_selector.h"
#include "mapdata.h"
#include "material.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 "pldata.h"
#include "recipe_dictionary.h"
#include "requirements.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 "vehicle.h"
#include "vehicle_selector.h"
#include "vitamin.h"
#include "weather.h"
#include "enums.h"
#include "int_id.h"
#include "inventory.h"
#include "item_location.h"
#include "json.h"
#include "line.h"
#include "player_activity.h"
#include "recipe.h"
#include "rng.h"
#include "flat_set.h"
#include "point.h"
#include "clothing_mod.h"
#include "cata_string_consts.h"
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 )
{
target = obj.get_string( "target" ); // required
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.empty() && !container.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 && 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( "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;
if( container.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.empty() ) {
obj->ammo_set( ammo_type, qty );
} else if( obj->ammo_current() != "null" ) {
obj->ammo_set( obj->ammo_current(), qty );
} else {
obj->set_countdown( qty );
}
}
} else {
it.convert( container );
obj = &it.emplace_back( target, calendar::turn, std::max( ammo_qty, 1 ) );
}
if( p.is_worn( *obj ) ) {
p.reset_encumbrance();
p.update_bodytemp();
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
{
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( ngettext( "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( !item::type_is_defined( target ) ) {
debugmsg( "Invalid transform target: %s", target.c_str() );
}
if( !container.empty() ) {
if( !item::type_is_defined( container ) ) {
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.item_tags.insert( "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> 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 )
{
std::vector<tripoint> result;
for( const auto &p : closest_tripoints_first( center, radius ) ) {
if( g->m.impassable( p ) ) {
continue;
}
if( p != center ) {
if( !g->m.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;
}
if( explosion.power >= 0.0f ) {
explosion_handler::explosion( pos, explosion );
}
if( draw_explosion_radius >= 0 ) {
explosion_handler::draw_explosion( pos, draw_explosion_radius, draw_explosion_color );
}
if( do_flashbang ) {
explosion_handler::flashbang( pos, flashbang_player_immune );
}
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 );
g->m.add_field( gas_source, fields_type, field_intensity, 1_turns );
}
}
if( scrambler_blast_radius >= 0 ) {
for( const tripoint &dest : g->m.points_in_radius( pos, scrambler_blast_radius ) ) {
explosion_handler::scrambler_blast( dest );
}
}
if( emp_blast_radius >= 0 ) {
for( const tripoint &dest : g->m.points_in_radius( pos, emp_blast_radius ) ) {
explosion_handler::emp_blast( dest );
}
}
return 1;
}
void explosion_iuse::info( const item &, std::vector<iteminfo> &dump ) const
{
if( explosion.power <= 0 ) {
// TODO: List other effects, like EMP and clouds
return;
}
dump.emplace_back( "TOOL", _( "Power at <bold>epicenter</bold>: " ), explosion.power );
const auto &sd = explosion.shrapnel;
if( sd.casing_mass > 0 ) {
dump.emplace_back( "TOOL", _( "Casing <bold>mass</bold>: " ), sd.casing_mass );
dump.emplace_back( "TOOL", _( "Fragment <bold>mass</bold>: " ), sd.fragment_mass );
}
}
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 = g->m.add_vehicle( vehicle_id, p.pos(), 0, 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() );
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( auto &elem : veh->parts ) {
int tmp;
veh_data >> tmp;
veh->set_hp( elem, 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 < veh->parts.size(); i++ ) {
const vehicle_part &src = parts[i];
vehicle_part &dst = veh->parts[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 ) );
}
return effect_data( efftype_id( e.get_string( "id" ) ), time,
get_body_part_token( e.get_string( "bp", "NUM_BP" ) ), e.get_bool( "permanent", false ) );
}
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 = g->u.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( "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( "syringe" ) && p.has_bionic( bio_syringe ) ) {
need_these.erase( "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, eff.permanent );
}
for( const auto &stat_adjustment : stat_adjustments ) {
p.mod_stat( stat_adjustment.first, stat_adjustment.second );
}
for( const auto &field : fields_produced ) {
const field_type_id fid = field_type_id( field.first );
for( int i = 0; i < 3; i++ ) {
g->m.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> place_monster_iuse::clone() const
{
return std::make_unique<place_monster_iuse>( *this );
}
void place_monster_iuse::load( const JsonObject &obj )
{
mtypeid = mtype_id( obj.get_string( "monster_id" ) );
obj.read( "friendly_msg", friendly_msg );
obj.read( "hostile_msg", hostile_msg );
obj.read( "difficulty", difficulty );
obj.read( "moves", moves );
obj.read( "place_randomly", place_randomly );
skill1 = skill_id( obj.get_string( "skill1", skill1.str() ) );
skill2 = skill_id( obj.get_string( "skill2", skill2.str() ) );
}
int place_monster_iuse::use( player &p, item &it, bool, const tripoint & ) const
{
shared_ptr_fast<monster> newmon_ptr = make_shared_fast<monster>( mtypeid );
monster &newmon = *newmon_ptr;
newmon.init_from_item( it );
if( place_randomly ) {
// place_critter_around returns the same pointer as its parameter (or null)
if( !g->place_critter_around( newmon_ptr, p.pos(), 1 ) ) {
p.add_msg_if_player( m_info, _( "There is no adjacent square to release the %s in!" ),
newmon.name() );
return 0;
}
} else {
const std::string query = string_format( _( "Place the %s where?" ), newmon.name() );
const cata::optional<tripoint> pnt_ = choose_adjacent( query );
if( !pnt_ ) {
return 0;
}
// place_critter_at returns the same pointer as its parameter (or null)
if( !g->place_critter_at( newmon_ptr, *pnt_ ) ) {
p.add_msg_if_player( m_info, _( "You cannot place a %s there." ), newmon.name() );
return 0;
}
}
p.moves -= moves;
if( !newmon.has_flag( MF_INTERIOR_AMMO ) ) {
for( auto &amdef : newmon.ammo ) {
item ammo_item( amdef.first, 0 );
const int available = p.charges_of( amdef.first );
if( available == 0 ) {
amdef.second = 0;
p.add_msg_if_player( m_info,
_( "If you had standard factory-built %1$s bullets, you could load the %2$s." ),
ammo_item.type_name( 2 ), newmon.name() );
continue;
}
// Don't load more than the default from the monster definition.
ammo_item.charges = std::min( available, amdef.second );
p.use_charges( amdef.first, ammo_item.charges );
//~ First %s is the ammo item (with plural form and count included), second is the monster name
p.add_msg_if_player( ngettext( "You load %1$d x %2$s round into the %3$s.",
"You load %1$d x %2$s rounds into the %3$s.", ammo_item.charges ),
ammo_item.charges, ammo_item.type_name( ammo_item.charges ),
newmon.name() );
amdef.second = ammo_item.charges;
}
}
int skill_offset = 0;
if( skill1 ) {
skill_offset += p.get_skill_level( skill1 ) / 2;
}
if( skill2 ) {
skill_offset += p.get_skill_level( skill2 );
}
/** @EFFECT_INT increases chance of a placed turret being friendly */
if( rng( 0, p.int_cur / 2 ) + skill_offset < rng( 0, difficulty ) ) {
if( hostile_msg.empty() ) {
p.add_msg_if_player( m_bad, _( "The %s scans you and makes angry beeping noises!" ),
newmon.name() );
} else {
p.add_msg_if_player( m_bad, "%s", _( hostile_msg ) );
}
} else {
if( friendly_msg.empty() ) {
p.add_msg_if_player( m_warning, _( "The %s emits an IFF beep as it scans you." ),
newmon.name() );
} else {
p.add_msg_if_player( m_warning, "%s", _( friendly_msg ) );
}
newmon.friendly = -1;
}
// TODO: add a flag instead of monster id or something?
if( newmon.type->id == mtype_id( "mon_laserturret" ) && !g->is_in_sunlight( newmon.pos() ) ) {
p.add_msg_if_player( _( "A flashing LED on the laser turret appears to indicate low light." ) );
}
return 1;
}
std::unique_ptr<iuse_actor> ups_based_armor_actor::clone() const
{
return std::make_unique<ups_based_armor_actor>( *this );
}
std::unique_ptr<iuse_actor> place_npc_iuse::clone() const
{
return std::make_unique<place_npc_iuse>( *this );
}
void place_npc_iuse::load( const JsonObject &obj )
{
npc_class_id = string_id<npc_template>( obj.get_string( "npc_class_id" ) );
obj.read( "summon_msg", summon_msg );
obj.read( "moves", moves );
obj.read( "place_randomly", place_randomly );
}
int place_npc_iuse::use( player &p, item &, bool, const tripoint & ) const
{
cata::optional<tripoint> target_pos;
if( place_randomly ) {
const tripoint_range target_range = points_in_radius( p.pos(), 1 );
target_pos = random_point( target_range, []( const tripoint & t ) {
return !g->m.passable( t );
} );
} else {
const std::string query = _( "Place npc where?" );
target_pos = choose_adjacent( _( "Place npc where?" ) );
}
if( !target_pos ) {
return 0;
}
if( !g->m.passable( target_pos.value() ) ) {
p.add_msg_if_player( m_info, _( "There is no square to spawn npc in!" ) );
return 0;
}
g->m.place_npc( target_pos.value().xy(), npc_class_id );
p.mod_moves( -moves );
p.add_msg_if_player( m_info, "%s", _( summon_msg ) );
return 1;
}
void ups_based_armor_actor::load( const JsonObject &obj )
{
obj.read( "activate_msg", activate_msg );
obj.read( "deactive_msg", deactive_msg );
obj.read( "out_of_power_msg", out_of_power_msg );
}
static bool has_powersource( const item &i, const player &p )
{
if( i.is_power_armor() && p.can_interface_armor() && p.has_power() ) {
return true;
}
return p.has_charges( "UPS", 1 );
}
int ups_based_armor_actor::use( player &p, item &it, bool t, const tripoint & ) const
{
if( t ) {
// Normal, continuous usage, do nothing. The item is *not* charge-based.
return 0;
}
if( p.get_item_position( &it ) >= -1 ) {
p.add_msg_if_player( m_info, _( "You should wear the %s before activating it." ),
it.tname() );
return 0;
}
if( !it.active && !has_powersource( it, p ) ) {
p.add_msg_if_player( m_info,
_( "You need some source of power for your %s (a simple UPS will do)." ), it.tname() );
if( it.is_power_armor() ) {
p.add_msg_if_player( m_info,
_( "There is also a certain bionic that helps with this kind of armor." ) );
}
return 0;
}
it.active = !it.active;
p.reset_encumbrance();
if( it.active ) {
if( activate_msg.empty() ) {
p.add_msg_if_player( m_info, _( "You activate your %s." ), it.tname() );
} else {
p.add_msg_if_player( m_info, _( activate_msg ), it.tname() );
}
} else {
if( deactive_msg.empty() ) {
p.add_msg_if_player( m_info, _( "You deactivate your %s." ), it.tname() );
} else {
p.add_msg_if_player( m_info, _( deactive_msg ), it.tname() );
}
}
return 0;
}
std::unique_ptr<iuse_actor> pick_lock_actor::clone() const
{
return std::make_unique<pick_lock_actor>( *this );
}
void pick_lock_actor::load( const JsonObject &obj )
{
pick_quality = obj.get_int( "pick_quality" );
}
int pick_lock_actor::use( player &p, item &it, bool, const tripoint & ) const
{
if( p.is_npc() ) {
return 0;
}
if( p.is_mounted() ) {
p.add_msg_if_player( m_info, _( "You cannot do that while mounted." ) );
return 0;
}
const std::set<ter_id> allowed_ter_id {
t_chaingate_l,
t_door_locked,
t_door_locked_alarm,
t_door_locked_interior,
t_door_locked_peep,
t_door_metal_pickable,
t_door_bar_locked
};
const std::function<bool( const tripoint & )> f = [&allowed_ter_id]( const tripoint & pnt ) {
if( pnt == g->u.pos() ) {
return false;
}
const ter_id type = g->m.ter( pnt );
const bool is_allowed_terrain = allowed_ter_id.find( type ) != allowed_ter_id.end();
return is_allowed_terrain;
};
const cata::optional<tripoint> pnt_ = choose_adjacent_highlight(
_( "Use your lockpick where?" ), f, false, true );
if( !pnt_ ) {
return 0;
}
const tripoint &pnt = *pnt_;
const ter_id type = g->m.ter( pnt );
if( !f( pnt ) ) {
if( pnt == p.pos() ) {
p.add_msg_if_player( m_info, _( "You pick your nose and your sinuses swing open." ) );
} else if( g->critter_at<npc>( pnt ) ) {
p.add_msg_if_player( m_info,
_( "You can pick your friends, and you can\npick your nose, but you can't pick\nyour friend's nose" ) );
} else if( type == t_door_c ) {
p.add_msg_if_player( m_info, _( "That door isn't locked." ) );
} else {
p.add_msg_if_player( m_info, _( "That cannot be picked." ) );
}
return 0;
}
ter_id new_type;
std::string open_message;
if( type == t_chaingate_l ) {
new_type = t_chaingate_c;
open_message = _( "With a satisfying click, the chain-link gate opens." );
} else if( type == t_door_locked || type == t_door_locked_alarm ||
type == t_door_locked_interior ) {
new_type = t_door_c;
open_message = _( "With a satisfying click, the lock on the door opens." );
} else if( type == t_door_locked_peep ) {
new_type = t_door_c_peep;
open_message = _( "With a satisfying click, the lock on the door opens." );
} else if( type == t_door_metal_pickable ) {
new_type = t_door_metal_c;
open_message = _( "With a satisfying click, the lock on the door opens." );
} else if( type == t_door_bar_locked ) {
new_type = t_door_bar_o;
//Bar doors auto-open (and lock if closed again) so show a different message)
open_message = _( "The door swings open…" );
} else {
return 0;
}
p.practice( skill_mechanics, 1 );