forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activity_actor.cpp
1311 lines (1110 loc) · 41.9 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 "activity_actor_definitions.h"
#include <algorithm>
#include <cmath>
#include <list>
#include <string>
#include <utility>
#include "avatar_action.h"
#include "activity_handlers.h" // put_into_vehicle_or_drop and drop_on_map
#include "advanced_inv.h"
#include "avatar.h"
#include "avatar_functions.h"
#include "calendar.h"
#include "character.h"
#include "character_functions.h"
#include "crafting.h"
#include "debug.h"
#include "enums.h"
#include "event.h"
#include "event_bus.h"
#include "game.h"
#include "gates.h"
#include "iexamine.h"
#include "int_id.h"
#include "item.h"
#include "item_group.h"
#include "item_location.h"
#include "json.h"
#include "line.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "npc.h"
#include "output.h"
#include "options.h"
#include "pickup.h"
#include "player.h"
#include "player_activity.h"
#include "point.h"
#include "ranged.h"
#include "recipe_dictionary.h"
#include "rng.h"
#include "sounds.h"
#include "timed_event.h"
#include "translations.h"
#include "uistate.h"
#include "units.h"
#include "vehicle.h"
#include "vpart_position.h"
static const itype_id itype_bone_human( "bone_human" );
static const itype_id itype_electrohack( "electrohack" );
static const skill_id skill_computer( "computer" );
static const mtype_id mon_zombie( "mon_zombie" );
static const mtype_id mon_zombie_fat( "mon_zombie_fat" );
static const mtype_id mon_zombie_rot( "mon_zombie_rot" );
static const mtype_id mon_skeleton( "mon_skeleton" );
static const mtype_id mon_zombie_crawler( "mon_zombie_crawler" );
static const std::string flag_RELOAD_AND_SHOOT( "RELOAD_AND_SHOOT" );
static const std::string has_thievery_witness( "has_thievery_witness" );
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 *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( !gun->ammo_remaining() && !reload_loc && gun->has_flag( flag_RELOAD_AND_SHOOT ) ) {
if( !load_RAS_weapon() ) {
aborted = true;
act.moves_left = 0;
return;
}
}
std::optional<shape_factory> shape_gen;
if( weapon->ammo_current() && weapon->ammo_current()->ammo &&
weapon->ammo_current()->ammo->shape ) {
shape_gen = weapon->ammo_current()->ammo->shape;
}
g->temp_exit_fullscreen();
target_handler::trajectory trajectory;
if( !shape_gen ) {
trajectory = target_handler::mode_fire( you, *this );
} else {
trajectory = target_handler::mode_shaped( you, *shape_gen, *this );
}
g->reenter_fullscreen();
if( aborted ) {
act.moves_left = 0;
} else {
if( !trajectory.empty() ) {
fin_trajectory = trajectory;
act.moves_left = 0;
}
// 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();
item *weapon = get_weapon();
if( !weapon ) {
restore_view();
return;
}
if( aborted ) {
if( reload_requested ) {
// Reload the gun / select different arrows
// May assign ACT_RELOAD
avatar_action::reload_wielded( true );
}
restore_view();
return;
}
// Fire!
gun_mode gun = weapon->gun_current_mode();
int shots_fired = ranged::fire_gun( who, fin_trajectory.back(), gun.qty, *gun, reload_loc );
if( shots_fired > 0 ) {
// TODO: bionic power cost of firing should be derived from a value of the relevant weapon.
if( bp_cost_per_shot > 0_J ) {
who.mod_power_level( -bp_cost_per_shot * shots_fired );
}
if( stamina_cost_per_shot > 0 ) {
who.mod_stamina( -stamina_cost_per_shot * shots_fired );
}
}
if( !get_option<bool>( "AIM_AFTER_FIRING" ) ) {
restore_view();
return;
}
// re-enter aiming UI with same parameters
aim_activity_actor aim_actor;
aim_actor.abort_if_no_targets = true;
aim_actor.fake_weapon = this->fake_weapon;
aim_actor.bp_cost_per_shot = this->bp_cost_per_shot;
aim_actor.initial_view_offset = this->initial_view_offset;
// if invalid target or it's dead - reset it so a new one is acquired
shared_ptr_fast<Creature> last_target = who.as_player()->last_target.lock();
if( last_target && last_target->is_dead_state() ) {
who.as_player()->last_target.reset();
}
who.assign_activity( player_activity( aim_actor ), false );
}
void aim_activity_actor::canceled( player_activity &/*act*/, Character &/*who*/ )
{
restore_view();
}
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( "stamina_cost_per_shot", stamina_cost_per_shot );
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( "loaded_RAS_weapon", loaded_RAS_weapon );
jsout.member( "reload_loc", reload_loc );
jsout.member( "aborted", aborted );
jsout.member( "reload_requested", reload_requested );
jsout.member( "abort_if_no_targets", abort_if_no_targets );
jsout.end_object();
}
std::unique_ptr<activity_actor> aim_activity_actor::deserialize( JsonIn &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( "stamina_cost_per_shot", actor.stamina_cost_per_shot );
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( "loaded_RAS_weapon", actor.loaded_RAS_weapon );
data.read( "reload_loc", actor.reload_loc );
data.read( "aborted", actor.aborted );
data.read( "reload_requested", actor.reload_requested );
data.read( "abort_if_no_targets", actor.abort_if_no_targets );
return actor.clone();
}
item *aim_activity_actor::get_weapon()
{
if( fake_weapon.has_value() ) {
// TODO: check if the player lost relevant bionic/mutation
return &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
item *weapon = &get_player_character().primary_weapon();
return weapon->is_null() ? nullptr : weapon;
}
}
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
player &you = get_avatar();
item *weapon = get_weapon();
gun_mode gun = weapon->gun_current_mode();
// Will burn (0.2% max base stamina * the strength required to fire)
stamina_cost_per_shot = gun->get_min_str() * static_cast<int>
( 0.002f * get_option<int>( "PLAYER_MAX_STAMINA" ) );
if( you.get_stamina() < stamina_cost_per_shot ) {
you.add_msg_if_player( m_bad, _( "You're too tired to draw your %s." ), weapon->tname() );
return false;
}
const auto ammo_location_is_valid = [&]() -> bool {
you.ammo_location.make_dirty();
if( !you.ammo_location )
{
you.ammo_location = item_location();
return false;
}
if( !gun->can_reload_with( you.ammo_location->typeId() ) )
{
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 ) : character_funcs::select_ammo( you, *gun );
if( !opt ) {
// Menu canceled
return false;
}
reload_loc = opt.ammo;
loaded_RAS_weapon = true;
return true;
}
void autodrive_activity_actor::start( player_activity &act, 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 ) ) {
who.cancel_activity();
return;
}
player_vehicle = &vp->vehicle();
player_vehicle->is_autodriving = true;
act.moves_left = calendar::INDEFINITELY_LONG;
}
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 )
{
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( JsonIn & )
{
return autodrive_activity_actor().clone();
}
void dig_activity_actor::start( player_activity &act, Character & )
{
act.moves_total = moves_total;
act.moves_left = moves_total;
}
void dig_activity_actor::do_turn( player_activity &, Character & )
{
sfx::play_activity_sound( "tool", "shovel", sfx::get_heard_volume( location ) );
if( calendar::once_every( 1_minutes ) ) {
//~ Sound of a shovel digging a pit at work!
sounds::sound( location, 10, sounds::sound_t::activity, _( "hsh!" ) );
}
}
void dig_activity_actor::finish( player_activity &act, Character &who )
{
map &here = get_map();
const bool grave = here.ter( location ) == t_grave;
if( grave ) {
if( one_in( 10 ) ) {
static const std::array<mtype_id, 5> monids = { {
mon_zombie, mon_zombie_fat,
mon_zombie_rot, mon_skeleton,
mon_zombie_crawler
}
};
g->place_critter_at( random_entry( monids ), byproducts_location );
here.furn_set( location, f_coffin_o );
who.add_msg_if_player( m_warning, _( "Something crawls out of the coffin!" ) );
} else {
here.spawn_item( location, itype_bone_human, rng( 5, 15 ) );
here.furn_set( location, f_coffin_c );
}
std::vector<item *> dropped = g->m.place_items( item_group_id( "allclothes" ), 50, location,
location, false,
calendar::turn );
g->m.place_items( item_group_id( "grave" ), 25, location, location, false, calendar::turn );
g->m.place_items( item_group_id( "jewelry_front" ), 20, location, location, false, calendar::turn );
for( item * const &it : dropped ) {
if( it->is_armor() ) {
it->item_tags.insert( "FILTHY" );
it->set_damage( rng( 1, it->max_damage() - 1 ) );
}
}
g->events().send<event_type::exhumes_grave>( who.getID() );
}
here.ter_set( location, ter_id( result_terrain ) );
for( int i = 0; i < byproducts_count; i++ ) {
here.spawn_items( byproducts_location,
item_group::items_from( item_group_id( byproducts_item_group ),
calendar::turn ) );
}
const int helpersize = character_funcs::get_crafting_helpers( who, 3 ).size();
who.mod_stored_nutr( 5 - helpersize );
who.mod_thirst( 5 - helpersize );
who.mod_fatigue( 10 - ( helpersize * 2 ) );
if( grave ) {
who.add_msg_if_player( m_good, _( "You finish exhuming a grave." ) );
} else {
who.add_msg_if_player( m_good, _( "You finish digging the %s." ),
here.ter( location ).obj().name() );
}
act.set_to_null();
}
void dig_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "moves", moves_total );
jsout.member( "location", location );
jsout.member( "result_terrain", result_terrain );
jsout.member( "byproducts_location", byproducts_location );
jsout.member( "byproducts_count", byproducts_count );
jsout.member( "byproducts_item_group", byproducts_item_group );
jsout.end_object();
}
std::unique_ptr<activity_actor> dig_activity_actor::deserialize( JsonIn &jsin )
{
dig_activity_actor actor( 0, tripoint_zero,
{}, tripoint_zero, 0, {} );
JsonObject data = jsin.get_object();
data.read( "moves", actor.moves_total );
data.read( "location", actor.location );
data.read( "result_terrain", actor.result_terrain );
data.read( "byproducts_location", actor.byproducts_location );
data.read( "byproducts_count", actor.byproducts_count );
data.read( "byproducts_item_group", actor.byproducts_item_group );
return actor.clone();
}
void dig_channel_activity_actor::start( player_activity &act, Character & )
{
act.moves_total = moves_total;
act.moves_left = moves_total;
}
void dig_channel_activity_actor::do_turn( player_activity &, Character & )
{
sfx::play_activity_sound( "tool", "shovel", sfx::get_heard_volume( location ) );
if( calendar::once_every( 1_minutes ) ) {
//~ Sound of a shovel digging a pit at work!
sounds::sound( location, 10, sounds::sound_t::activity, _( "hsh!" ) );
}
}
void dig_channel_activity_actor::finish( player_activity &act, Character &who )
{
map &here = get_map();
here.ter_set( location, ter_id( result_terrain ) );
for( int i = 0; i < byproducts_count; i++ ) {
here.spawn_items( byproducts_location,
item_group::items_from( item_group_id( byproducts_item_group ),
calendar::turn ) );
}
who.mod_stored_kcal( -40 );
who.mod_thirst( 5 );
who.mod_fatigue( 10 );
who.add_msg_if_player( m_good, _( "You finish digging up %s." ),
here.ter( location ).obj().name() );
act.set_to_null();
}
void dig_channel_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "moves", moves_total );
jsout.member( "location", location );
jsout.member( "result_terrain", result_terrain );
jsout.member( "byproducts_location", byproducts_location );
jsout.member( "byproducts_count", byproducts_count );
jsout.member( "byproducts_item_group", byproducts_item_group );
jsout.end_object();
}
std::unique_ptr<activity_actor> dig_channel_activity_actor::deserialize( JsonIn &jsin )
{
dig_channel_activity_actor actor( 0, tripoint_zero,
{}, tripoint_zero, 0, {} );
JsonObject data = jsin.get_object();
data.read( "moves", actor.moves_total );
data.read( "location", actor.location );
data.read( "result_terrain", actor.result_terrain );
data.read( "byproducts_location", actor.byproducts_location );
data.read( "byproducts_count", actor.byproducts_count );
data.read( "byproducts_item_group", actor.byproducts_item_group );
return actor.clone();
}
bool disassemble_activity_actor::try_start_single( player_activity &act, Character &who )
{
if( targets.empty() ) {
return false;
}
const iuse_location &target = targets.front();
if( !target.loc ) {
debugmsg( "Lost target of ACT_DISASSEMBLE" );
targets.clear();
return false;
}
const item &itm = *target.loc;
const recipe &dis = recipe_dictionary::get_uncraft( itm.typeId() );
// Have to check here again in case we ran out of tools
const ret_val<bool> can_do = crafting::can_disassemble( who, itm, who.crafting_inventory() );
if( !can_do.success() ) {
who.add_msg_if_player( m_info, "%s", can_do.c_str() );
return false;
}
int moves_needed = dis.time * target.count;
act.moves_total = moves_needed;
act.moves_left = moves_needed;
return true;
}
int disassemble_activity_actor::calc_num_targets() const
{
return static_cast<int>( targets.size() );
}
void disassemble_activity_actor::start( player_activity &act, Character &who )
{
if( !who.is_avatar() ) {
debugmsg( "ACT_DISASSEMBLE is not implemented for NPCs" );
act.set_to_null();
} else if( !try_start_single( act, who ) ) {
act.set_to_null();
}
initial_num_targets = calc_num_targets();
}
void disassemble_activity_actor::finish( player_activity &act, Character &who )
{
const iuse_location &target = targets.front();
if( !target.loc ) {
debugmsg( "Lost target of ACT_DISASSEMBLY" );
} else {
crafting::complete_disassemble( who, target, get_map().getlocal( pos.raw() ) );
}
targets.erase( targets.begin() );
if( try_start_single( act, who ) ) {
return;
}
// Make a copy to avoid use-after-free
bool recurse = this->recursive;
act.set_to_null();
if( recurse ) {
crafting::disassemble_all( *who.as_avatar(), recurse );
}
}
void disassemble_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "targets", targets );
jsout.member( "pos", pos );
jsout.member( "recursive", recursive );
jsout.member( "initial_num_targets", initial_num_targets );
jsout.end_object();
}
std::unique_ptr<activity_actor> disassemble_activity_actor::deserialize( JsonIn &jsin )
{
disassemble_activity_actor actor;
JsonObject data = jsin.get_object();
data.read( "targets", actor.targets );
data.read( "pos", actor.pos );
data.read( "recursive", actor.recursive );
data.read( "initial_num_targets", actor.initial_num_targets );
return actor.clone();
}
act_progress_message disassemble_activity_actor::get_progress_message(
const player_activity &act, const Character & ) const
{
std::string msg;
const int percentage = ( ( act.moves_total - act.moves_left ) * 100 ) / act.moves_total;
msg = string_format( "%d%%", percentage );
if( initial_num_targets != 1 ) {
constexpr int width = 20; // An arbitrary value
std::string item_name_trimmed = trim_by_length( targets.front().loc->display_name(), width );
msg += string_format(
_( "\n%d out of %d, working on %-20s" ),
initial_num_targets - calc_num_targets() + 1,
initial_num_targets,
item_name_trimmed
);
}
return act_progress_message::make_extra_info( std::move( msg ) );
}
drop_activity_actor::drop_activity_actor( Character &ch, const drop_locations &items,
bool force_ground, const tripoint &relpos )
: force_ground( force_ground ), relpos( relpos )
{
this->items = pickup::reorder_for_dropping( ch, items );
}
void drop_activity_actor::start( player_activity &act, Character & )
{
// Set moves_left to value other than zero to indicate ongoing activity
act.moves_total = 1;
act.moves_left = 1;
}
void drop_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "items", items );
jsout.member( "force_ground", force_ground );
jsout.member( "relpos", relpos );
jsout.end_object();
}
std::unique_ptr<activity_actor> drop_activity_actor::deserialize( JsonIn &jsin )
{
drop_activity_actor actor;
JsonObject data = jsin.get_object();
data.read( "items", actor.items );
data.read( "force_ground", actor.force_ground );
data.read( "relpos", actor.relpos );
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 hack_result {
HACK_UNABLE,
HACK_FAIL,
HACK_NOTHING,
HACK_SUCCESS
};
enum hack_type {
HACK_SAFE,
HACK_DOOR,
HACK_GAS,
HACK_NULL
};
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, const bool using_bionic )
{
// TODO: Remove this once player -> Character migration is complete
{
player *p = dynamic_cast<player *>( &who );
p->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!" ) );
if( using_bionic ) {
who.mod_power_level( -25_kJ );
} else {
who.use_charges( itype_electrohack, 25 );
}
if( success <= -5 ) {
if( !using_bionic ) {
who.add_msg_if_player( m_bad, _( "Your electrohack is ruined!" ) );
who.use_amount( itype_electrohack, 1 );
} else {
who.add_msg_if_player( m_bad, _( "Your power is drained!" ) );
who.mod_power_level( units::from_kilojoule( -rng( 25,
units::to_kilojoule( who.get_power_level() ) ) ) );
}
}
return HACK_FAIL;
} else if( success < 6 ) {
return HACK_NOTHING;
} else {
return HACK_SUCCESS;
}
}
static hack_type get_hack_type( tripoint examp )
{
hack_type type = HACK_NULL;
const 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.examine == &iexamine::pay_gas || xfurn_t.examine == &iexamine::pay_gas ) {
type = HACK_GAS;
} else if( xter_t.examine == &iexamine::cardreader || xfurn_t.examine == &iexamine::cardreader ) {
type = HACK_DOOR;
} else if( xter_t.examine == &iexamine::gunsafe_el || xfurn_t.examine == &iexamine::gunsafe_el ) {
type = HACK_SAFE;
}
return type;
}
hacking_activity_actor::hacking_activity_actor( use_bionic )
: using_bionic( true )
{
}
void hacking_activity_actor::finish( player_activity &act, Character &who )
{
tripoint examp = act.placement;
hack_type type = get_hack_type( examp );
map &here = get_map();
switch( hack_attempt( who, using_bionic ) ) {
case HACK_UNABLE:
who.add_msg_if_player( _( "You cannot hack this." ) );
break;
case HACK_FAIL:
// currently all things that can be hacked have equivalent alarm failure states.
// this may not always be the case with new hackable things.
g->events().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 && !g->timed_events.queued( TIMED_EVENT_WANTED ) ) {
g->timed_events.add( TIMED_EVENT_WANTED, calendar::turn + 30_minutes, 0,
who.global_sm_location() );
}
break;
case HACK_NOTHING:
who.add_msg_if_player( _( "You fail the hack, but no alarms are triggered." ) );
break;
case HACK_SUCCESS:
if( type == HACK_GAS ) {
int tankGasUnits;
const std::optional<tripoint> pTank_ = iexamine::getNearFilledGasTank( examp, tankGasUnits );
if( !pTank_ ) {
break;
}
const tripoint pTank = *pTank_;
const std::optional<tripoint> pGasPump = iexamine::getGasPumpByNumber( examp,
uistate.ags_pay_gas_selected_pump );
if( pGasPump && iexamine::toPumpFuel( pTank, *pGasPump, tankGasUnits ) ) {
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_SAFE ) {
who.add_msg_if_player( m_good, _( "The door on the safe swings open." ) );
here.furn_set( examp, furn_str_id( "f_safe_o" ) );
} else if( type == HACK_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 &jsout ) const
{
jsout.start_object();
jsout.member( "using_bionic", using_bionic );
jsout.end_object();
}
std::unique_ptr<activity_actor> hacking_activity_actor::deserialize( JsonIn &jsin )
{
hacking_activity_actor actor;
if( jsin.test_null() ) {
// Old saves might contain a null instead of an object.
// Since we do not know whether a bionic or an item was chosen we assume
// it was an item.
actor.using_bionic = false;
} else {
JsonObject jsobj = jsin.get_object();
jsobj.read( "using_bionic", actor.using_bionic );
}
return actor.clone();
}
void move_items_activity_actor::do_turn( player_activity &act, Character &who )
{
const tripoint dest = relative_destination + who.pos();
while( who.moves > 0 && !target_items.empty() ) {
item_location target = std::move( target_items.back() );
const int quantity = quantities.back();
target_items.pop_back();
quantities.pop_back();
if( !target ) {
debugmsg( "Lost target item of ACT_MOVE_ITEMS" );
continue;
}
// Don't need to make a copy here since movement can't be canceled
item &leftovers = *target;
// Make a copy to be put in the destination location
item newit = leftovers;
// Check that we can pick it up.
if( !newit.made_of( LIQUID ) ) {
// This is for hauling across zlevels, remove when going up and down stairs
// is no longer teleportation
if( newit.is_owned_by( who, true ) ) {
newit.set_owner( who );
} else {
continue;
}
// Handle charges, quantity == 0 means move all
if( quantity != 0 && newit.count_by_charges() ) {
newit.charges = std::min( newit.charges, quantity );
leftovers.charges -= quantity;
} else {
leftovers.charges = 0;
}
const tripoint src = target.position();
const int distance = src.z == dest.z ? std::max( rl_dist( src, dest ), 1 ) : 1;
who.mod_moves( -pickup::cost_to_move_item( who, newit ) * distance );
if( to_vehicle ) {
put_into_vehicle_or_drop( who, item_drop_reason::deliberate, { newit }, dest );
} else {
drop_on_map( who, item_drop_reason::deliberate, { newit }, dest );
}
// If we picked up a whole stack, remove the leftover item
if( leftovers.charges <= 0 ) {
target.remove_item();
}
}
}
if( target_items.empty() ) {
// Nuke the current activity, leaving the backlog alone.
act.set_to_null();
}
}
void move_items_activity_actor::serialize( JsonOut &jsout ) const
{
jsout.start_object();
jsout.member( "target_items", target_items );
jsout.member( "quantities", quantities );
jsout.member( "to_vehicle", to_vehicle );
jsout.member( "relative_destination", relative_destination );
jsout.end_object();
}
std::unique_ptr<activity_actor> move_items_activity_actor::deserialize( JsonIn &jsin )
{
move_items_activity_actor actor( {}, {}, false, tripoint_zero );
JsonObject data = jsin.get_object();
data.read( "target_items", actor.target_items );
data.read( "quantities", actor.quantities );
data.read( "to_vehicle", actor.to_vehicle );
data.read( "relative_destination", actor.relative_destination );
return actor.clone();
}
void pickup_activity_actor::do_turn( player_activity &act, Character &who )
{
// If we don't have target items bail out
if( target_items.empty() ) {
who.cancel_activity();
return;
}
// If the player moves while picking up (i.e.: in a moving vehicle) cancel
// the activity, only populate starting_pos when grabbing from the ground
if( starting_pos && *starting_pos != who.pos() ) {
who.cancel_activity();
who.add_msg_if_player( _( "Moving canceled auto-pickup." ) );
return;
}
// Auto_resume implies autopickup.
const bool autopickup = who.activity.auto_resume;
// False indicates that the player canceled pickup when met with some prompt
const bool keep_going = pickup::do_pickup( target_items, autopickup );
// Check thievey witness
npc *witness = nullptr;
if( !act.str_values.empty() && act.str_values[0] == has_thievery_witness ) {