forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity_actor.cpp
6116 lines (5237 loc) · 208 KB
/
activity_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 "activity_actor.h"
#include <algorithm>
#include <array>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <functional>
#include <list>
#include <map>
#include <new>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "action.h"
#include "activity_actor_definitions.h"
#include "activity_handlers.h" // put_into_vehicle_or_drop and drop_on_map
#include "advanced_inv.h"
#include "avatar.h"
#include "avatar_action.h"
#include "bodypart.h"
#include "character.h"
#include "coordinates.h"
#include "contents_change_handler.h"
#include "craft_command.h"
#include "creature_tracker.h"
#include "debug.h"
#include "enums.h"
#include "event.h"
#include "event_bus.h"
#include "field_type.h"
#include "flag.h"
#include "game.h"
#include "game_constants.h"
#include "game_inventory.h"
#include "gates.h"
#include "gun_mode.h"
#include "handle_liquid.h"
#include "harvest.h"
#include "iexamine.h"
#include "item.h"
#include "item_group.h"
#include "item_location.h"
#include "itype.h"
#include "iuse_actor.h"
#include "json.h"
#include "line.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "memory_fast.h"
#include "martialarts.h"
#include "messages.h"
#include "monster.h"
#include "morale_types.h"
#include "mtype.h"
#include "npc.h"
#include "optional.h"
#include "options.h"
#include "output.h"
#include "pickup.h"
#include "pimpl.h"
#include "player_activity.h"
#include "point.h"
#include "ranged.h"
#include "recipe.h"
#include "requirements.h"
#include "ret_val.h"
#include "rng.h"
#include "shearing.h"
#include "sounds.h"
#include "string_formatter.h"
#include "skill.h"
#include "timed_event.h"
#include "translations.h"
#include "ui.h"
#include "uistate.h"
#include "units.h"
#include "value_ptr.h"
#include "vehicle.h"
#include "vpart_position.h"
static const activity_id ACT_AIM( "ACT_AIM" );
static const activity_id ACT_AUTODRIVE( "ACT_AUTODRIVE" );
static const activity_id ACT_BIKERACK_RACKING( "ACT_BIKERACK_RACKING" );
static const activity_id ACT_BIKERACK_UNRACKING( "ACT_BIKERACK_UNRACKING" );
static const activity_id ACT_BINDER_COPY_RECIPE( "ACT_BINDER_COPY_RECIPE" );
static const activity_id ACT_BOLTCUTTING( "ACT_BOLTCUTTING" );
static const activity_id ACT_CHOP_LOGS( "ACT_CHOP_LOGS" );
static const activity_id ACT_CHOP_PLANKS( "ACT_CHOP_PLANKS" );
static const activity_id ACT_CHOP_TREE( "ACT_CHOP_TREE" );
static const activity_id ACT_CHURN( "ACT_CHURN" );
static const activity_id ACT_CLEAR_RUBBLE( "ACT_CLEAR_RUBBLE" );
static const activity_id ACT_CONSUME( "ACT_CONSUME" );
static const activity_id ACT_CONSUME_MEDS_MENU( "ACT_CONSUME_MEDS_MENU" );
static const activity_id ACT_CRACKING( "ACT_CRACKING" );
static const activity_id ACT_CRAFT( "ACT_CRAFT" );
static const activity_id ACT_DISABLE( "ACT_DISABLE" );
static const activity_id ACT_DISASSEMBLE( "ACT_DISASSEMBLE" );
static const activity_id ACT_DROP( "ACT_DROP" );
static const activity_id ACT_EAT_MENU( "ACT_EAT_MENU" );
static const activity_id ACT_EBOOKSAVE( "ACT_EBOOKSAVE" );
static const activity_id ACT_FIRSTAID( "ACT_FIRSTAID" );
static const activity_id ACT_FORAGE( "ACT_FORAGE" );
static const activity_id ACT_FURNITURE_MOVE( "ACT_FURNITURE_MOVE" );
static const activity_id ACT_GUNMOD_ADD( "ACT_GUNMOD_ADD" );
static const activity_id ACT_GUNMOD_REMOVE( "ACT_GUNMOD_REMOVE" );
static const activity_id ACT_HACKING( "ACT_HACKING" );
static const activity_id ACT_HACKSAW( "ACT_HACKSAW" );
static const activity_id ACT_HAIRCUT( "ACT_HAIRCUT" );
static const activity_id ACT_HARVEST( "ACT_HARVEST" );
static const activity_id ACT_HOTWIRE_CAR( "ACT_HOTWIRE_CAR" );
static const activity_id ACT_INSERT_ITEM( "ACT_INSERT_ITEM" );
static const activity_id ACT_LOCKPICK( "ACT_LOCKPICK" );
static const activity_id ACT_LONGSALVAGE( "ACT_LONGSALVAGE" );
static const activity_id ACT_MEDITATE( "ACT_MEDITATE" );
static const activity_id ACT_MIGRATION_CANCEL( "ACT_MIGRATION_CANCEL" );
static const activity_id ACT_MILK( "ACT_MILK" );
static const activity_id ACT_MOP( "ACT_MOP" );
static const activity_id ACT_MOVE_ITEMS( "ACT_MOVE_ITEMS" );
static const activity_id ACT_MULTIPLE_CHOP_TREES( "ACT_MULTIPLE_CHOP_TREES" );
static const activity_id ACT_OPEN_GATE( "ACT_OPEN_GATE" );
static const activity_id ACT_OXYTORCH( "ACT_OXYTORCH" );
static const activity_id ACT_PICKUP( "ACT_PICKUP" );
static const activity_id ACT_PICKUP_MENU( "ACT_PICKUP_MENU" );
static const activity_id ACT_PLAY_WITH_PET( "ACT_PLAY_WITH_PET" );
static const activity_id ACT_PRYING( "ACT_PRYING" );
static const activity_id ACT_READ( "ACT_READ" );
static const activity_id ACT_RELOAD( "ACT_RELOAD" );
static const activity_id ACT_SHAVE( "ACT_SHAVE" );
static const activity_id ACT_SHEARING( "ACT_SHEARING" );
static const activity_id ACT_STASH( "ACT_STASH" );
static const activity_id ACT_TENT_DECONSTRUCT( "ACT_TENT_DECONSTRUCT" );
static const activity_id ACT_TENT_PLACE( "ACT_TENT_PLACE" );
static const activity_id ACT_TRY_SLEEP( "ACT_TRY_SLEEP" );
static const activity_id ACT_UNLOAD( "ACT_UNLOAD" );
static const activity_id ACT_WEAR( "ACT_WEAR" );
static const activity_id ACT_WIELD( "ACT_WIELD" );
static const activity_id ACT_WORKOUT_ACTIVE( "ACT_WORKOUT_ACTIVE" );
static const activity_id ACT_WORKOUT_HARD( "ACT_WORKOUT_HARD" );
static const activity_id ACT_WORKOUT_LIGHT( "ACT_WORKOUT_LIGHT" );
static const activity_id ACT_WORKOUT_MODERATE( "ACT_WORKOUT_MODERATE" );
static const ammotype ammo_plutonium( "plutonium" );
static const efftype_id effect_docile( "docile" );
static const efftype_id effect_paid( "paid" );
static const efftype_id effect_pet( "pet" );
static const efftype_id effect_sensor_stun( "sensor_stun" );
static const efftype_id effect_sheared( "sheared" );
static const efftype_id effect_sleep( "sleep" );
static const efftype_id effect_tied( "tied" );
static const efftype_id effect_took_thorazine( "took_thorazine" );
static const efftype_id effect_worked_on( "worked_on" );
static const faction_id faction_your_followers( "your_followers" );
static const furn_str_id furn_f_gunsafe_mj( "f_gunsafe_mj" );
static const furn_str_id furn_f_safe_o( "f_safe_o" );
static const gun_mode_id gun_mode_DEFAULT( "DEFAULT" );
static const item_group_id Item_spawn_data_forage_autumn( "forage_autumn" );
static const item_group_id Item_spawn_data_forage_spring( "forage_spring" );
static const item_group_id Item_spawn_data_forage_summer( "forage_summer" );
static const item_group_id Item_spawn_data_forage_winter( "forage_winter" );
static const item_group_id Item_spawn_data_trash_forest( "trash_forest" );
static const itype_id itype_2x4( "2x4" );
static const itype_id itype_disassembly( "disassembly" );
static const itype_id itype_electrohack( "electrohack" );
static const itype_id itype_log( "log" );
static const itype_id itype_paper( "paper" );
static const itype_id itype_pseudo_bio_picklock( "pseudo_bio_picklock" );
static const itype_id itype_splinter( "splinter" );
static const itype_id itype_stick_long( "stick_long" );
static const json_character_flag json_flag_SUPER_HEARING( "SUPER_HEARING" );
static const move_mode_id move_mode_prone( "prone" );
static const move_mode_id move_mode_walk( "walk" );
static const mtype_id mon_manhack( "mon_manhack" );
static const proficiency_id proficiency_prof_lockpicking( "prof_lockpicking" );
static const proficiency_id proficiency_prof_lockpicking_expert( "prof_lockpicking_expert" );
static const proficiency_id proficiency_prof_safecracking( "prof_safecracking" );
static const quality_id qual_LOCKPICK( "LOCKPICK" );
static const quality_id qual_PRY( "PRY" );
static const quality_id qual_PRYING_NAIL( "PRYING_NAIL" );
static const quality_id qual_SAW_M( "SAW_M" );
static const quality_id qual_SHEAR( "SHEAR" );
static const skill_id skill_computer( "computer" );
static const skill_id skill_electronics( "electronics" );
static const skill_id skill_fabrication( "fabrication" );
static const skill_id skill_mechanics( "mechanics" );
static const skill_id skill_survival( "survival" );
static const skill_id skill_traps( "traps" );
static const ter_str_id ter_t_underbrush_harvested_autumn( "t_underbrush_harvested_autumn" );
static const ter_str_id ter_t_underbrush_harvested_spring( "t_underbrush_harvested_spring" );
static const ter_str_id ter_t_underbrush_harvested_summer( "t_underbrush_harvested_summer" );
static const ter_str_id ter_t_underbrush_harvested_winter( "t_underbrush_harvested_winter" );
static const trait_id trait_SCHIZOPHRENIC( "SCHIZOPHRENIC" );
std::string activity_actor::get_progress_message( const player_activity &act ) const
{
if( act.moves_total > 0 ) {
const int pct = ( ( act.moves_total - act.moves_left ) * 100 ) / act.moves_total;
return string_format( "%d%%", pct );
} else {
return std::string();
}
}
aim_activity_actor::aim_activity_actor()
{
initial_view_offset = get_avatar().view_offset;
}
aim_activity_actor aim_activity_actor::use_wielded()
{
return aim_activity_actor();
}
aim_activity_actor aim_activity_actor::use_bionic( const item &fake_gun,
const units::energy &cost_per_shot )
{
aim_activity_actor act = aim_activity_actor();
act.bp_cost_per_shot = cost_per_shot;
act.fake_weapon = fake_gun;
return act;
}
aim_activity_actor aim_activity_actor::use_mutation( const item &fake_gun )
{
aim_activity_actor act = aim_activity_actor();
act.fake_weapon = fake_gun;
return act;
}
void aim_activity_actor::start( player_activity &act, Character &/*who*/ )
{
// Time spent on aiming is determined on the go by the player
act.moves_total = 1;
act.moves_left = 1;
}
void aim_activity_actor::do_turn( player_activity &act, Character &who )
{
if( !who.is_avatar() ) {
debugmsg( "ACT_AIM not implemented for NPCs" );
aborted = true;
act.moves_left = 0;
return;
}
avatar &you = get_avatar();
item_location weapon = get_weapon();
if( !weapon || !avatar_action::can_fire_weapon( you, get_map(), *weapon ) ) {
aborted = true;
act.moves_left = 0;
return;
}
gun_mode gun = weapon->gun_current_mode();
if( first_turn && gun->has_flag( flag_RELOAD_AND_SHOOT ) && !gun->ammo_remaining() ) {
if( !load_RAS_weapon() ) {
aborted = true;
act.moves_left = 0;
return;
}
}
g->temp_exit_fullscreen();
target_handler::trajectory trajectory = target_handler::mode_fire( you, *this );
g->reenter_fullscreen();
if( aborted ) {
act.moves_left = 0;
} else {
if( !trajectory.empty() ) {
fin_trajectory = trajectory;
act.moves_left = 0;
}
// If aborting on the first turn, keep 'first_turn' as 'true'.
// This allows refunding moves spent on unloading RELOAD_AND_SHOOT weapons
// to simulate avatar not loading them in the first place
first_turn = false;
// Allow interrupting activity only during 'aim and fire'.
// Prevents '.' key for 'aim for 10 turns' from conflicting with '.' key for 'interrupt activity'
// in case of high input lag (curses, sdl sometimes...), but allows to interrupt aiming
// if a bug happens / stars align to cause an endless aiming loop.
act.interruptable_with_kb = action != "AIM";
}
}
void aim_activity_actor::finish( player_activity &act, Character &who )
{
act.set_to_null();
restore_view();
item_location weapon = get_weapon();
if( !weapon ) {
return;
}
if( aborted ) {
unload_RAS_weapon();
if( reload_requested ) {
// Reload the gun / select different arrows
// May assign ACT_RELOAD
g->reload_wielded( true );
}
return;
}
// Fire!
gun_mode gun = weapon->gun_current_mode();
int shots_fired = who.fire_gun( fin_trajectory.back(), gun.qty, *gun );
// TODO: bionic power cost of firing should be derived from a value of the relevant weapon.
if( shots_fired && ( bp_cost_per_shot > 0_J ) ) {
who.mod_power_level( -bp_cost_per_shot * shots_fired );
}
}
void aim_activity_actor::canceled( player_activity &/*act*/, Character &/*who*/ )
{
restore_view();
unload_RAS_weapon();
}
void aim_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "fake_weapon", fake_weapon );
jsout.member( "bp_cost_per_shot", bp_cost_per_shot );
jsout.member( "fin_trajectory", fin_trajectory );
jsout.member( "first_turn", first_turn );
jsout.member( "action", action );
jsout.member( "aif_duration", aif_duration );
jsout.member( "aiming_at_critter", aiming_at_critter );
jsout.member( "snap_to_target", snap_to_target );
jsout.member( "shifting_view", shifting_view );
jsout.member( "initial_view_offset", initial_view_offset );
jsout.member( "aborted", aborted );
jsout.member( "reload_requested", reload_requested );
jsout.end_object();
}
std::unique_ptr<activity_actor> aim_activity_actor::deserialize( JsonValue &jsin )
{
aim_activity_actor actor = aim_activity_actor();
JsonObject data = jsin.get_object();
data.read( "fake_weapon", actor.fake_weapon );
data.read( "bp_cost_per_shot", actor.bp_cost_per_shot );
data.read( "fin_trajectory", actor.fin_trajectory );
data.read( "first_turn", actor.first_turn );
data.read( "action", actor.action );
data.read( "aif_duration", actor.aif_duration );
data.read( "aiming_at_critter", actor.aiming_at_critter );
data.read( "snap_to_target", actor.snap_to_target );
data.read( "shifting_view", actor.shifting_view );
data.read( "initial_view_offset", actor.initial_view_offset );
data.read( "aborted", actor.aborted );
data.read( "reload_requested", actor.reload_requested );
return actor.clone();
}
item_location aim_activity_actor::get_weapon()
{
if( fake_weapon.has_value() ) {
// TODO: check if the player lost relevant bionic/mutation
return item_location( get_player_character(), &fake_weapon.value() );
} else {
// Check for lost gun (e.g. yanked by zombie technician)
// TODO: check that this is the same gun that was used to start aiming
return get_player_character().get_wielded_item();
}
}
void aim_activity_actor::restore_view()
{
avatar &player_character = get_avatar();
bool changed_z = player_character.view_offset.z != initial_view_offset.z;
player_character.view_offset = initial_view_offset;
if( changed_z ) {
get_map().invalidate_map_cache( player_character.view_offset.z );
g->invalidate_main_ui_adaptor();
}
}
bool aim_activity_actor::load_RAS_weapon()
{
// TODO: use activity for fetching ammo and loading weapon
Character &you = get_avatar();
item_location weapon = get_weapon();
gun_mode gun = weapon->gun_current_mode();
const auto ammo_location_is_valid = [&]() -> bool {
if( !you.ammo_location )
{
return false;
}
if( !gun->can_reload_with( *you.ammo_location.get_item(), false ) )
{
return false;
}
if( square_dist( you.pos(), you.ammo_location.position() ) > 1 )
{
return false;
}
return true;
};
item::reload_option opt = ammo_location_is_valid() ? item::reload_option( &you, &*weapon,
&*weapon, you.ammo_location ) : you.select_ammo( *gun );
if( !opt ) {
// Menu canceled
return false;
}
int reload_time = 0;
reload_time += opt.moves();
if( !gun->reload( you, std::move( opt.ammo ), 1 ) ) {
// Reload not allowed
return false;
}
// Burn 0.6% max base stamina without cardio/BMI factored in x the strength required to fire.
you.mod_stamina( gun->get_min_str() * static_cast<int>( 0.006f *
get_option<int>( "PLAYER_MAX_STAMINA_BASE" ) ) );
// At low stamina levels, firing starts getting slow.
int sta_percent = ( 100 * you.get_stamina() ) / you.get_stamina_max();
reload_time += ( sta_percent < 25 ) ? ( ( 25 - sta_percent ) * 2 ) : 0;
you.moves -= reload_time;
return true;
}
void aim_activity_actor::unload_RAS_weapon()
{
// Unload reload-and-shoot weapons to avoid leaving bows pre-loaded with arrows
avatar &you = get_avatar();
item_location weapon = get_weapon();
if( !weapon ) {
return;
}
gun_mode gun = weapon->gun_current_mode();
if( gun->has_flag( flag_RELOAD_AND_SHOOT ) ) {
int moves_before_unload = you.moves;
// Note: this code works only for avatar
item_location loc = item_location( you, gun.target );
you.unload( loc, true );
// Give back time for unloading as essentially nothing has been done.
if( first_turn ) {
you.moves = moves_before_unload;
}
}
}
void autodrive_activity_actor::update_player_vehicle( Character &who )
{
const bool in_vehicle = who.in_vehicle && who.controlling_vehicle;
const optional_vpart_position vp = get_map().veh_at( who.pos() );
if( !( vp && in_vehicle ) ) {
player_vehicle = nullptr;
} else {
player_vehicle = &vp->vehicle();
}
}
void autodrive_activity_actor::start( player_activity &, Character &who )
{
update_player_vehicle( who );
if( player_vehicle == nullptr ) {
who.cancel_activity();
} else {
player_vehicle->is_autodriving = true;
}
}
void autodrive_activity_actor::do_turn( player_activity &act, Character &who )
{
if( who.in_vehicle && who.controlling_vehicle && player_vehicle ) {
if( who.moves <= 0 ) {
// out of moves? the driver's not doing anything this turn
// (but the vehicle will continue moving)
return;
}
switch( player_vehicle->do_autodrive( who ) ) {
case autodrive_result::ok:
if( who.moves > 0 ) {
// if do_autodrive() didn't eat up all our moves, end the turn
// equivalent to player pressing the "pause" button
who.moves = 0;
}
sounds::reset_markers();
break;
case autodrive_result::abort:
who.cancel_activity();
break;
case autodrive_result::finished:
act.moves_left = 0;
break;
}
} else {
who.cancel_activity();
}
}
void autodrive_activity_actor::canceled( player_activity &act, Character &who )
{
// The activity might be canceled because the driver got unboarded, in which case we may not have a valid pointer anymore (e.g. summoned vehicle).
update_player_vehicle( who );
who.add_msg_if_player( m_info, _( "Auto drive canceled." ) );
who.omt_path.clear();
if( player_vehicle ) {
player_vehicle->stop_autodriving( false );
}
act.set_to_null();
}
void autodrive_activity_actor::finish( player_activity &act, Character &who )
{
who.add_msg_if_player( m_info, _( "You have reached your destination." ) );
player_vehicle->stop_autodriving( false );
act.set_to_null();
}
void autodrive_activity_actor::serialize( JsonOut &jsout ) const
{
// Activity is not being saved but still provide some valid json if called.
jsout.write_null();
}
std::unique_ptr<activity_actor> autodrive_activity_actor::deserialize( JsonValue & )
{
return autodrive_activity_actor().clone();
}
void gunmod_remove_activity_actor::start( player_activity &act, Character & )
{
act.moves_total = moves_total;
act.moves_left = moves_total;
}
void gunmod_remove_activity_actor::finish( player_activity &act, Character &who )
{
item *it_gun = gun.get_item();
if( !it_gun ) {
debugmsg( "ACT_GUNMOD_REMOVE lost target gun" );
act.set_to_null();
return;
}
item *it_mod = nullptr;
std::vector<item *> mods = it_gun->gunmods();
if( gunmod_idx >= 0 && mods.size() > static_cast<size_t>( gunmod_idx ) ) {
it_mod = mods[gunmod_idx];
} else {
debugmsg( "ACT_GUNMOD_REMOVE lost target gunmod" );
act.set_to_null();
return;
}
act.set_to_null();
gunmod_remove( who, *it_gun, *it_mod );
}
bool gunmod_remove_activity_actor::gunmod_unload( Character &who, item &gunmod )
{
if( gunmod.has_flag( flag_BRASS_CATCHER ) ) {
// Exclude brass catchers so that removing them wouldn't spill the casings
return true;
}
// TODO: unloading gunmods happens instantaneously in some cases, but should take time
item_location loc = item_location( who, &gunmod );
return !( gunmod.ammo_remaining() && !who.unload( loc, true ) );
}
void gunmod_remove_activity_actor::gunmod_remove( Character &who, item &gun, item &mod )
{
if( !gunmod_unload( who, mod ) ) {
return;
}
gun.gun_set_mode( gun_mode_DEFAULT );
const itype *modtype = mod.type;
who.i_add_or_drop( mod );
gun.remove_item( mod );
// If the removed gunmod added mod locations, check to see if any mods are in invalid locations
if( !modtype->gunmod->add_mod.empty() ) {
std::map<gunmod_location, int> mod_locations = gun.get_mod_locations();
for( const auto &slot : mod_locations ) {
int free_slots = gun.get_free_mod_locations( slot.first );
for( item *the_mod : gun.gunmods() ) {
if( the_mod->type->gunmod->location == slot.first && free_slots < 0 ) {
gunmod_remove( who, gun, *the_mod );
free_slots++;
} else if( mod_locations.find( the_mod->type->gunmod->location ) ==
mod_locations.end() ) {
gunmod_remove( who, gun, *the_mod );
}
}
}
}
//~ %1$s - gunmod, %2$s - gun.
who.add_msg_if_player( _( "You remove your %1$s from your %2$s." ), modtype->nname( 1 ),
gun.tname() );
}
void gunmod_remove_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "moves_total", moves_total );
jsout.member( "gun", gun );
jsout.member( "gunmod", gunmod_idx );
jsout.end_object();
}
std::unique_ptr<activity_actor> gunmod_remove_activity_actor::deserialize( JsonValue &jsin )
{
gunmod_remove_activity_actor actor( 0, item_location(), -1 );
JsonObject data = jsin.get_object();
data.read( "moves_total", actor.moves_total );
data.read( "gun", actor.gun );
data.read( "gunmod", actor.gunmod_idx );
return actor.clone();
}
void hacking_activity_actor::start( player_activity &act, Character & )
{
act.moves_total = to_moves<int>( 5_minutes );
act.moves_left = to_moves<int>( 5_minutes );
}
enum class hack_result : int {
UNABLE,
FAIL,
NOTHING,
SUCCESS
};
enum class hack_type : int {
SAFE,
DOOR,
GAS,
NONE
};
static int hack_level( const Character &who )
{
///\EFFECT_COMPUTER increases success chance of hacking card readers
// odds go up with int>8, down with int<8
// 4 int stat is worth 1 computer skill here
///\EFFECT_INT increases success chance of hacking card readers
return who.get_skill_level( skill_computer ) + who.int_cur / 2 - 8;
}
static hack_result hack_attempt( Character &who )
{
// TODO: Remove this once player -> Character migration is complete
{
who.practice( skill_computer, 20 );
}
// only skilled supergenius never cause short circuits, but the odds are low for people
// with moderate skills
const int hack_stddev = 5;
int success = std::ceil( normal_roll( hack_level( who ), hack_stddev ) );
if( success < 0 ) {
who.add_msg_if_player( _( "You cause a short circuit!" ) );
who.use_charges( itype_electrohack, 25 );
if( success <= -5 ) {
who.use_charges( itype_electrohack, 50 );
}
return hack_result::FAIL;
} else if( success < 6 ) {
return hack_result::NOTHING;
} else {
return hack_result::SUCCESS;
}
}
static hack_type get_hack_type( const tripoint &examp )
{
hack_type type = hack_type::NONE;
map &here = get_map();
const furn_t &xfurn_t = here.furn( examp ).obj();
const ter_t &xter_t = here.ter( examp ).obj();
if( xter_t.has_examine( iexamine::pay_gas ) || xfurn_t.has_examine( iexamine::pay_gas ) ) {
type = hack_type::GAS;
} else if( xter_t.has_examine( "cardreader" ) ||
xfurn_t.has_examine( "cardreader" ) ) {
type = hack_type::DOOR;
} else if( xter_t.has_examine( iexamine::gunsafe_el ) ||
xfurn_t.has_examine( iexamine::gunsafe_el ) ) {
type = hack_type::SAFE;
}
return type;
}
void hacking_activity_actor::finish( player_activity &act, Character &who )
{
tripoint examp = act.placement;
hack_type type = get_hack_type( examp );
switch( hack_attempt( who ) ) {
case hack_result::UNABLE:
who.add_msg_if_player( _( "You cannot hack this." ) );
break;
case hack_result::FAIL:
// currently all things that can be hacked have equivalent alarm failure states.
// this may not always be the case with new hackable things.
get_event_bus().send<event_type::triggers_alarm>( who.getID() );
sounds::sound( who.pos(), 60, sounds::sound_t::music, _( "an alarm sound!" ), true, "environment",
"alarm" );
if( examp.z > 0 && !get_timed_events().queued( timed_event_type::WANTED ) ) {
get_timed_events().add( timed_event_type::WANTED, calendar::turn + 30_minutes, 0,
who.get_location() );
}
break;
case hack_result::NOTHING:
who.add_msg_if_player( _( "You fail the hack, but no alarms are triggered." ) );
break;
case hack_result::SUCCESS:
map &here = get_map();
if( type == hack_type::GAS ) {
int tankUnits;
fuel_station_fuel_type fuelType;
const cata::optional<tripoint> pTank_ = iexamine::getNearFilledGasTank( examp, tankUnits,
fuelType );
if( !pTank_ ) {
break;
}
const tripoint pTank = *pTank_;
const cata::optional<tripoint> pGasPump = iexamine::getGasPumpByNumber( examp,
uistate.ags_pay_gas_selected_pump );
if( pGasPump && iexamine::toPumpFuel( pTank, *pGasPump, tankUnits ) ) {
who.add_msg_if_player( _( "You hack the terminal and route all available fuel to your pump!" ) );
sounds::sound( examp, 6, sounds::sound_t::activity,
_( "Glug Glug Glug Glug Glug Glug Glug Glug Glug" ), true, "tool", "gaspump" );
} else {
who.add_msg_if_player( _( "Nothing happens." ) );
}
} else if( type == hack_type::SAFE ) {
who.add_msg_if_player( m_good, _( "The door on the safe swings open." ) );
here.furn_set( examp, furn_f_safe_o );
} else if( type == hack_type::DOOR ) {
who.add_msg_if_player( _( "You activate the panel!" ) );
who.add_msg_if_player( m_good, _( "The nearby doors unlock." ) );
here.ter_set( examp, t_card_reader_broken );
for( const tripoint &tmp : here.points_in_radius( examp, 3 ) ) {
if( here.ter( tmp ) == t_door_metal_locked ) {
here.ter_set( tmp, t_door_metal_c );
}
}
}
break;
}
act.set_to_null();
}
void hacking_activity_actor::serialize( JsonOut & ) const {}
std::unique_ptr<activity_actor> hacking_activity_actor::deserialize( JsonValue & )
{
hacking_activity_actor actor;
return actor.clone();
}
void bookbinder_copy_activity_actor::start( player_activity &act, Character & )
{
pages = 1 + rec_id->difficulty / 2;
act.moves_total = to_moves<int>( pages * 10_minutes );
act.moves_left = to_moves<int>( pages * 10_minutes );
}
void bookbinder_copy_activity_actor::do_turn( player_activity &, Character &p )
{
if( p.fine_detail_vision_mod() > 4.0f ) {
p.cancel_activity();
p.add_msg_if_player( m_info, _( "It's too dark to write!" ) );
return;
}
}
void bookbinder_copy_activity_actor::finish( player_activity &act, Character &p )
{
if( !book_binder->can_contain( item( itype_paper, calendar::turn, pages ) ).success() ) {
debugmsg( "Book binder can not contain '%s' recipe when it should.", rec_id.str() );
act.set_to_null();
return;
}
const bool rec_added = book_binder->eipc_recipe_add( rec_id );
if( rec_added ) {
p.add_msg_if_player( m_good, _( "You copy the recipe for %1$s into your recipe book." ),
rec_id->result_name() );
p.use_charges( itype_paper, pages );
const std::vector<const item *> writing_tools_filter =
p.crafting_inventory().items_with( [&]( const item & it ) {
return it.has_flag( flag_WRITE_MESSAGE ) && it.ammo_remaining() >= it.ammo_required() ;
} );
std::vector<tool_comp> writing_tools;
writing_tools.reserve( writing_tools_filter.size() );
for( const item *tool : writing_tools_filter ) {
writing_tools.emplace_back( tool_comp( tool->typeId(), 1 ) );
}
p.consume_tools( writing_tools, pages );
book_binder->put_in( item( itype_paper, calendar::turn, pages ),
item_pocket::pocket_type::MAGAZINE );
} else {
debugmsg( "Recipe book already has '%s' recipe when it should not.", rec_id.str() );
}
act.set_to_null();
}
void bookbinder_copy_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "book_binder", book_binder );
jsout.member( "rec_id", rec_id );
jsout.member( "pages", pages );
jsout.end_object();
}
std::unique_ptr<activity_actor> bookbinder_copy_activity_actor::deserialize( JsonValue &jsin )
{
bookbinder_copy_activity_actor actor;
JsonObject jsobj = jsin.get_object();
jsobj.read( "book_binder", actor.book_binder );
jsobj.read( "rec_id", actor.rec_id );
jsobj.read( "pages", actor.pages );
return actor.clone();
}
void hotwire_car_activity_actor::start( player_activity &act, Character & )
{
act.moves_total = moves_total;
act.moves_left = moves_total;
}
void hotwire_car_activity_actor::do_turn( player_activity &act, Character & )
{
map &here = get_map();
if( calendar::once_every( 1_minutes ) ) {
bool lost = !here.veh_at( here.getlocal( target ) ).has_value();
if( lost ) {
act.set_to_null();
debugmsg( "Lost ACT_HOTWIRE_CAR target vehicle" );
}
}
}
void hotwire_car_activity_actor::finish( player_activity &act, Character &who )
{
act.set_to_null();
map &here = get_map();
const optional_vpart_position vp = here.veh_at( here.getlocal( target ) );
if( !vp ) {
debugmsg( "Lost ACT_HOTWIRE_CAR target vehicle" );
return;
}
vehicle &veh = vp->vehicle();
int skill = who.get_skill_level( skill_mechanics );
if( skill > rng( 1, 6 ) ) {
// Success
who.add_msg_if_player( _( "You found the wire that starts the engine." ) );
veh.is_locked = false;
} else if( skill > rng( 0, 4 ) ) {
// Soft fail
who.add_msg_if_player( _( "You found a wire that looks like the right one." ) );
veh.is_alarm_on = veh.has_security_working();
veh.is_locked = false;
} else if( !veh.is_alarm_on ) {
// Hard fail
who.add_msg_if_player( _( "The red wire always starts the engine, doesn't it?" ) );
veh.is_alarm_on = veh.has_security_working();
} else {
// Already failed
who.add_msg_if_player(
_( "By process of elimination, you found the wire that starts the engine." ) );
veh.is_locked = false;
}
}
void hotwire_car_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "target", target );
jsout.member( "moves_total", moves_total );
jsout.end_object();
}
std::unique_ptr<activity_actor> hotwire_car_activity_actor::deserialize( JsonValue &jsin )
{
hotwire_car_activity_actor actor( 0, tripoint_zero );
JsonObject data = jsin.get_object();
data.read( "target", actor.target );
data.read( "moves_total", actor.moves_total );
return actor.clone();
}
void hacksaw_activity_actor::start( player_activity &act, Character &/*who*/ )
{
const map &here = get_map();
int moves_before_quality;
if( here.has_furn( target ) ) {
const furn_id furn_type = here.furn( target );
if( !furn_type->hacksaw->valid() ) {
if( !testing ) {
debugmsg( "%s hacksaw is invalid", furn_type.id().str() );
}
act.set_to_null();
return;
}
moves_before_quality = to_moves<int>( furn_type->hacksaw->duration() );
} else if( !here.ter( target )->is_null() ) {
const ter_id ter_type = here.ter( target );
if( !ter_type->hacksaw->valid() ) {
if( !testing ) {
debugmsg( "%s hacksaw is invalid", ter_type.id().str() );
}
act.set_to_null();
return;
}
moves_before_quality = to_moves<int>( ter_type->hacksaw->duration() );
} else {
if( !testing ) {
debugmsg( "hacksaw activity called on invalid terrain" );
}
act.set_to_null();
return;
}
const int qual = tool->get_quality( qual_SAW_M );
if( qual < 2 ) {
if( !testing ) {
debugmsg( "Item %s with 'HACKSAW' use action requires SAW_M quality of at least 2.",
tool->typeId().c_str() );
}
act.set_to_null();
return;
}
//Speed of hacksaw action is the SAW_M quality over 2, 2 being the hacksaw level.
//3 makes the speed one-and-a-half times, and the total moves 66% of hacksaw. 4 is twice the speed, 50% the moves, etc, 5 is 2.5 times the speed, 40% the original time
//done because it's easy to code, and diminising returns on cutting speed make sense as the limiting factor becomes aligning the tool and controlling it instead of the actual cutting
act.moves_total = moves_before_quality / ( qual / 2 );
add_msg_debug( debugmode::DF_ACTIVITY, "%s moves_total: %d", act.id().str(), act.moves_total );
act.moves_left = act.moves_total;
}
void hacksaw_activity_actor::do_turn( player_activity &/*act*/, Character &who )
{
std::string method = "HACKSAW";
if( tool->ammo_sufficient( &who, method ) ) {
int ammo_consumed = tool->ammo_required();
std::map<std::string, float>::const_iterator iter = tool->type->ammo_scale.find( method );
if( iter != tool->type->ammo_scale.end() ) {
ammo_consumed *= iter->second;
}