-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathavatar_action.cpp
1304 lines (1172 loc) · 45 KB
/
avatar_action.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 "avatar_action.h"
#include <cstdlib>
#include <algorithm>
#include <functional>
#include <memory>
#include <ostream>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "action.h"
#include "avatar.h"
#include "creature.h"
#include "game.h"
#include "game_inventory.h"
#include "input.h"
#include "item.h"
#include "item_location.h"
#include "itype.h"
#include "line.h"
#include "map.h"
#include "mapdata.h"
#include "map_iterator.h"
#include "messages.h"
#include "monster.h"
#include "npc.h"
#include "options.h"
#include "output.h"
#include "projectile.h"
#include "ranged.h"
#include "translations.h"
#include "type_id.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "bodypart.h"
#include "cursesdef.h"
#include "debug.h"
#include "enums.h"
#include "game_constants.h"
#include "gun_mode.h"
#include "int_id.h"
#include "inventory.h"
#include "item_location.h"
#include "mtype.h"
#include "player_activity.h"
#include "ret_val.h"
#include "rng.h"
static const activity_id ACT_AIM( "ACT_AIM" );
static const efftype_id effect_amigara( "amigara" );
static const efftype_id effect_glowing( "glowing" );
static const efftype_id effect_harnessed( "harnessed" );
static const efftype_id effect_onfire( "onfire" );
static const efftype_id effect_pet( "pet" );
static const efftype_id effect_relax_gas( "relax_gas" );
static const efftype_id effect_ridden( "ridden" );
static const efftype_id effect_stunned( "stunned" );
static const skill_id skill_swimming( "swimming" );
static const bionic_id bio_ups( "bio_ups" );
static const trait_id trait_BURROW( "BURROW" );
static const trait_id trait_GRAZER( "GRAZER" );
static const trait_id trait_RUMINANT( "RUMINANT" );
static const trait_id trait_SHELL2( "SHELL2" );
static const std::string flag_ALLOWS_REMOTE_USE( "ALLOWS_REMOTE_USE" );
static const std::string flag_DIG_TOOL( "DIG_TOOL" );
static const std::string flag_FIRE_TWOHAND( "FIRE_TWOHAND" );
static const std::string flag_MOUNTABLE( "MOUNTABLE" );
static const std::string flag_MOUNTED_GUN( "MOUNTED_GUN" );
static const std::string flag_NO_UNWIELD( "NO_UNWIELD" );
static const std::string flag_RAMP_END( "RAMP_END" );
static const std::string flag_RELOAD_AND_SHOOT( "RELOAD_AND_SHOOT" );
static const std::string flag_RESTRICT_HANDS( "RESTRICT_HANDS" );
static const std::string flag_SWIMMABLE( "SWIMMABLE" );
#define dbg(x) DebugLog((x),D_SDL) << __FILE__ << ":" << __LINE__ << ": "
bool avatar_action::move( avatar &you, map &m, int dx, int dy, int dz )
{
if( ( !g->check_safe_mode_allowed() ) || you.has_active_mutation( trait_SHELL2 ) ) {
if( you.has_active_mutation( trait_SHELL2 ) ) {
add_msg( m_warning, _( "You can't move while in your shell. Deactivate it to go mobile." ) );
}
return false;
}
const bool is_riding = you.is_mounted();
tripoint dest_loc;
if( dz == 0 && you.has_effect( effect_stunned ) ) {
dest_loc.x = rng( you.posx() - 1, you.posx() + 1 );
dest_loc.y = rng( you.posy() - 1, you.posy() + 1 );
dest_loc.z = you.posz();
} else {
dest_loc.x = you.posx() + dx;
dest_loc.y = you.posy() + dy;
dest_loc.z = you.posz() + dz;
}
if( dest_loc == you.pos() ) {
// Well that sure was easy
return true;
}
if( m.has_flag( TFLAG_MINEABLE, dest_loc ) && g->mostseen == 0 &&
get_option<bool>( "AUTO_FEATURES" ) && get_option<bool>( "AUTO_MINING" ) &&
!m.veh_at( dest_loc ) && !you.is_underwater() && !you.has_effect( effect_stunned ) &&
!is_riding ) {
if( you.weapon.has_flag( flag_DIG_TOOL ) ) {
if( you.weapon.type->can_use( "JACKHAMMER" ) && you.weapon.ammo_sufficient() ) {
you.invoke_item( &you.weapon, "JACKHAMMER", dest_loc );
// don't move into the tile until done mining
you.defer_move( dest_loc );
return true;
} else if( you.weapon.type->can_use( "PICKAXE" ) ) {
you.invoke_item( &you.weapon, "PICKAXE", dest_loc );
// don't move into the tile until done mining
you.defer_move( dest_loc );
return true;
}
}
if( you.has_trait( trait_BURROW ) ) {
item burrowing_item( itype_id( "fake_burrowing" ) );
you.invoke_item( &burrowing_item, "BURROW", dest_loc );
// don't move into the tile until done mining
you.defer_move( dest_loc );
return true;
}
}
// by this point we're either walking, running, crouching, or attacking, so update the activity level to match
if( !is_riding ) {
if( you.movement_mode_is( CMM_WALK ) ) {
you.increase_activity_level( LIGHT_EXERCISE );
} else if( you.movement_mode_is( CMM_CROUCH ) ) {
you.increase_activity_level( MODERATE_EXERCISE );
} else {
you.increase_activity_level( ACTIVE_EXERCISE );
}
}
// If the player is *attempting to* move on the X axis, update facing direction of their sprite to match.
int new_dx = dest_loc.x - you.posx();
int new_dy = dest_loc.y - you.posy();
if( !tile_iso ) {
if( new_dx > 0 ) {
you.facing = FD_RIGHT;
if( is_riding ) {
you.mounted_creature->facing = FD_RIGHT;
}
} else if( new_dx < 0 ) {
you.facing = FD_LEFT;
if( is_riding ) {
you.mounted_creature->facing = FD_LEFT;
}
}
} else {
//
// iso:
//
// right key => +x -y FD_RIGHT
// left key => -x +y FD_LEFT
// up key => +x +y ______
// down key => -x -y ______
// y: left-up key => __ +y FD_LEFT
// u: right-up key => +x __ FD_RIGHT
// b: left-down key => -x __ FD_LEFT
// n: right-down key => __ -y FD_RIGHT
//
// right key => +x -y FD_RIGHT
// u: right-up key => +x __ FD_RIGHT
// n: right-down key => __ -y FD_RIGHT
// up key => +x +y ______
// down key => -x -y ______
// left key => -x +y FD_LEFT
// y: left-up key => __ +y FD_LEFT
// b: left-down key => -x __ FD_LEFT
//
// right key => +x +y FD_RIGHT
// u: right-up key => +x __ FD_RIGHT
// n: right-down key => __ +y FD_RIGHT
// up key => +x -y ______
// left key => -x -y FD_LEFT
// b: left-down key => -x __ FD_LEFT
// y: left-up key => __ -y FD_LEFT
// down key => -x +y ______
//
if( new_dx >= 0 && new_dy >= 0 ) {
you.facing = FD_RIGHT;
if( is_riding ) {
auto mons = you.mounted_creature.get();
mons->facing = FD_RIGHT;
}
}
if( new_dy <= 0 && new_dx <= 0 ) {
you.facing = FD_LEFT;
if( is_riding ) {
auto mons = you.mounted_creature.get();
mons->facing = FD_LEFT;
}
}
}
if( dz == 0 && ramp_move( you, m, dest_loc ) ) {
// TODO: Make it work nice with automove (if it doesn't do so already?)
return false;
}
if( you.has_effect( effect_amigara ) ) {
int curdist = INT_MAX;
int newdist = INT_MAX;
const tripoint minp = tripoint( 0, 0, you.posz() );
const tripoint maxp = tripoint( MAPSIZE_X, MAPSIZE_Y, you.posz() );
for( const tripoint &pt : m.points_in_rectangle( minp, maxp ) ) {
if( m.ter( pt ) == t_fault ) {
int dist = rl_dist( pt, you.pos() );
if( dist < curdist ) {
curdist = dist;
}
dist = rl_dist( pt, dest_loc );
if( dist < newdist ) {
newdist = dist;
}
}
}
if( newdist > curdist ) {
add_msg( m_info, _( "You cannot pull yourself away from the faultline…" ) );
return false;
}
}
dbg( D_PEDANTIC_INFO ) << "game:plmove: From (" <<
you.posx() << "," << you.posy() << "," << you.posz() << ") to (" <<
dest_loc.x << "," << dest_loc.y << "," << dest_loc.z << ")";
if( g->disable_robot( dest_loc ) ) {
return false;
}
// Check if our movement is actually an attack on a monster or npc
// Are we displacing a monster?
bool attacking = false;
if( g->critter_at( dest_loc ) ) {
attacking = true;
}
if( !you.move_effects( attacking ) ) {
you.moves -= 100;
return false;
}
if( monster *const mon_ptr = g->critter_at<monster>( dest_loc, true ) ) {
monster &critter = *mon_ptr;
if( critter.friendly == 0 &&
!critter.has_effect( effect_pet ) ) {
if( you.is_auto_moving() ) {
add_msg( m_warning, _( "Monster in the way. Auto-move canceled." ) );
add_msg( m_info, _( "Move into the monster to attack." ) );
you.clear_destination();
return false;
} else {
// fighting is hard work!
you.increase_activity_level( EXTRA_EXERCISE );
}
if( you.has_effect( effect_relax_gas ) ) {
if( one_in( 8 ) ) {
add_msg( m_good, _( "Your willpower asserts itself, and so do you!" ) );
} else {
you.moves -= rng( 2, 8 ) * 10;
add_msg( m_bad, _( "You're too pacified to strike anything…" ) );
return false;
}
}
you.melee_attack( critter, true );
if( critter.is_hallucination() ) {
critter.die( &you );
}
g->draw_hit_mon( dest_loc, critter, critter.is_dead() );
return false;
} else if( critter.has_flag( MF_IMMOBILE ) || critter.has_effect( effect_harnessed ) ||
critter.has_effect( effect_ridden ) ) {
add_msg( m_info, _( "You can't displace your %s." ), critter.name() );
return false;
}
// Successful displacing is handled (much) later
}
// If not a monster, maybe there's an NPC there
if( npc *const np_ = g->critter_at<npc>( dest_loc ) ) {
npc &np = *np_;
if( you.is_auto_moving() ) {
add_msg( _( "NPC in the way, Auto-move canceled." ) );
add_msg( m_info, _( "Move into the NPC to interact or attack." ) );
you.clear_destination();
return false;
}
if( !np.is_enemy() ) {
g->npc_menu( np );
return false;
}
you.melee_attack( np, true );
// fighting is hard work!
you.increase_activity_level( EXTRA_EXERCISE );
np.make_angry();
return false;
}
// GRAB: pre-action checking.
int dpart = -1;
const optional_vpart_position vp0 = m.veh_at( you.pos() );
vehicle *const veh0 = veh_pointer_or_null( vp0 );
const optional_vpart_position vp1 = m.veh_at( dest_loc );
vehicle *const veh1 = veh_pointer_or_null( vp1 );
bool veh_closed_door = false;
bool outside_vehicle = ( veh0 == nullptr || veh0 != veh1 );
if( veh1 != nullptr ) {
dpart = veh1->next_part_to_open( vp1->part_index(), outside_vehicle );
veh_closed_door = dpart >= 0 && !veh1->parts[dpart].open;
}
if( veh0 != nullptr && abs( veh0->velocity ) > 100 ) {
if( veh1 == nullptr ) {
if( query_yn( _( "Dive from moving vehicle?" ) ) ) {
g->moving_vehicle_dismount( dest_loc );
}
return false;
} else if( veh1 != veh0 ) {
add_msg( m_info, _( "There is another vehicle in the way." ) );
return false;
} else if( !vp1.part_with_feature( "BOARDABLE", true ) ) {
add_msg( m_info, _( "That part of the vehicle is currently unsafe." ) );
return false;
}
}
bool toSwimmable = m.has_flag( flag_SWIMMABLE, dest_loc );
bool toDeepWater = m.has_flag( TFLAG_DEEP_WATER, dest_loc );
bool fromSwimmable = m.has_flag( flag_SWIMMABLE, you.pos() );
bool fromDeepWater = m.has_flag( TFLAG_DEEP_WATER, you.pos() );
bool fromBoat = veh0 != nullptr && veh0->is_in_water();
bool toBoat = veh1 != nullptr && veh1->is_in_water();
if( is_riding ) {
if( !you.check_mount_will_move( dest_loc ) ) {
if( you.is_auto_moving() ) {
you.clear_destination();
}
you.moves -= 20;
return false;
}
}
// Dive into water!
if( toSwimmable && toDeepWater && !toBoat ) {
// Requires confirmation if we were on dry land previously
if( is_riding ) {
auto mon = you.mounted_creature.get();
if( !mon->swims() || mon->get_size() < you.get_size() + 2 ) {
add_msg( m_warning, _( "The %s cannot swim while it is carrying you!" ), mon->get_name() );
return false;
}
}
if( ( fromSwimmable && fromDeepWater && !fromBoat ) || query_yn( _( "Dive into the water?" ) ) ) {
if( ( !fromDeepWater || fromBoat ) && you.swim_speed() < 500 ) {
add_msg( _( "You start swimming." ) );
add_msg( m_info, _( "%s to dive underwater." ),
press_x( ACTION_MOVE_DOWN ) );
}
avatar_action::swim( g->m, g->u, dest_loc );
}
g->on_move_effects();
return true;
}
//Wooden Fence Gate (or equivalently walkable doors):
// open it if we are walking
// vault over it if we are running
if( m.passable_ter_furn( dest_loc )
&& you.movement_mode_is( CMM_WALK )
&& m.open_door( dest_loc, !m.is_outside( you.pos() ) ) ) {
you.moves -= 100;
// if auto-move is on, continue moving next turn
if( you.is_auto_moving() ) {
you.defer_move( dest_loc );
}
return true;
}
if( g->walk_move( dest_loc ) ) {
return true;
}
if( g->phasing_move( dest_loc ) ) {
return true;
}
if( veh_closed_door ) {
if( !veh1->handle_potential_theft( dynamic_cast<player &>( you ) ) ) {
return true;
} else {
if( outside_vehicle ) {
veh1->open_all_at( dpart );
} else {
veh1->open( dpart );
add_msg( _( "You open the %1$s's %2$s." ), veh1->name,
veh1->part_info( dpart ).name() );
}
}
you.moves -= 100;
// if auto-move is on, continue moving next turn
if( you.is_auto_moving() ) {
you.defer_move( dest_loc );
}
return true;
}
if( m.furn( dest_loc ) != f_safe_c && m.open_door( dest_loc, !m.is_outside( you.pos() ) ) ) {
you.moves -= 100;
// if auto-move is on, continue moving next turn
if( you.is_auto_moving() ) {
you.defer_move( dest_loc );
}
return true;
}
// Invalid move
const bool waste_moves = you.is_blind() || you.has_effect( effect_stunned );
if( waste_moves || dest_loc.z != you.posz() ) {
add_msg( _( "You bump into the %s!" ), m.obstacle_name( dest_loc ) );
// Only lose movement if we're blind
if( waste_moves ) {
you.moves -= 100;
}
} else if( m.ter( dest_loc ) == t_door_locked || m.ter( dest_loc ) == t_door_locked_peep ||
m.ter( dest_loc ) == t_door_locked_alarm || m.ter( dest_loc ) == t_door_locked_interior ) {
// Don't drain move points for learning something you could learn just by looking
add_msg( _( "That door is locked!" ) );
} else if( m.ter( dest_loc ) == t_door_bar_locked ) {
add_msg( _( "You rattle the bars but the door is locked!" ) );
}
return false;
}
bool avatar_action::ramp_move( avatar &you, map &m, const tripoint &dest_loc )
{
if( dest_loc.z != you.posz() ) {
// No recursive ramp_moves
return false;
}
// We're moving onto a tile with no support, check if it has a ramp below
if( !m.has_floor_or_support( dest_loc ) ) {
tripoint below( dest_loc.xy(), dest_loc.z - 1 );
if( m.has_flag( TFLAG_RAMP, below ) ) {
// But we're moving onto one from above
const tripoint dp = dest_loc - you.pos();
move( you, m, tripoint( dp.xy(), -1 ) );
// No penalty for misaligned stairs here
// Also cheaper than climbing up
return true;
}
return false;
}
if( !m.has_flag( TFLAG_RAMP, you.pos() ) ||
m.passable( dest_loc ) ) {
return false;
}
// Try to find an aligned end of the ramp that will make our climb faster
// Basically, finish walking on the stairs instead of pulling self up by hand
bool aligned_ramps = false;
for( const tripoint &pt : m.points_in_radius( you.pos(), 1 ) ) {
if( rl_dist( pt, dest_loc ) < 2 && m.has_flag( flag_RAMP_END, pt ) ) {
aligned_ramps = true;
break;
}
}
const tripoint above_u( you.posx(), you.posy(), you.posz() + 1 );
if( m.has_floor_or_support( above_u ) ) {
add_msg( m_warning, _( "You can't climb here - there's a ceiling above." ) );
return false;
}
const tripoint dp = dest_loc - you.pos();
const tripoint old_pos = you.pos();
move( you, m, tripoint( dp.xy(), 1 ) );
// We can't just take the result of the above function here
if( you.pos() != old_pos ) {
you.moves -= 50 + ( aligned_ramps ? 0 : 50 );
}
return true;
}
void avatar_action::swim( map &m, avatar &you, const tripoint &p )
{
if( !m.has_flag( flag_SWIMMABLE, p ) ) {
dbg( D_ERROR ) << "game:plswim: Tried to swim in "
<< m.tername( p ) << "!";
debugmsg( "Tried to swim in %s!", m.tername( p ) );
return;
}
if( you.has_effect( effect_onfire ) ) {
add_msg( _( "The water puts out the flames!" ) );
you.remove_effect( effect_onfire );
if( you.is_mounted() ) {
monster *mon = you.mounted_creature.get();
if( mon->has_effect( effect_onfire ) ) {
mon->remove_effect( effect_onfire );
}
}
}
if( you.has_effect( effect_glowing ) ) {
add_msg( _( "The water washes off the glowing goo!" ) );
you.remove_effect( effect_glowing );
}
int movecost = you.swim_speed();
you.practice( skill_swimming, you.is_underwater() ? 2 : 1 );
if( movecost >= 500 ) {
if( !you.is_underwater() && !( you.shoe_type_count( "swim_fins" ) == 2 ||
( you.shoe_type_count( "swim_fins" ) == 1 && one_in( 2 ) ) ) ) {
add_msg( m_bad, _( "You sink like a rock!" ) );
you.set_underwater( true );
///\EFFECT_STR increases breath-holding capacity while sinking
you.oxygen = 30 + 2 * you.str_cur;
}
}
if( you.oxygen <= 5 && you.is_underwater() ) {
if( movecost < 500 ) {
popup( _( "You need to breathe! (%s to surface.)" ), press_x( ACTION_MOVE_UP ) );
} else {
popup( _( "You need to breathe but you can't swim! Get to dry land, quick!" ) );
}
}
bool diagonal = ( p.x != you.posx() && p.y != you.posy() );
if( you.in_vehicle ) {
m.unboard_vehicle( you.pos() );
}
if( you.is_mounted() && m.veh_at( you.pos() ).part_with_feature( VPFLAG_BOARDABLE, true ) ) {
add_msg( m_warning, _( "You cannot board a vehicle while mounted." ) );
return;
}
if( const auto vp = m.veh_at( p ).part_with_feature( VPFLAG_BOARDABLE, true ) ) {
if( !vp->vehicle().handle_potential_theft( dynamic_cast<player &>( you ) ) ) {
return;
}
}
you.setpos( p );
g->update_map( you );
if( m.veh_at( you.pos() ).part_with_feature( VPFLAG_BOARDABLE, true ) ) {
m.board_vehicle( you.pos(), &you );
}
you.moves -= ( movecost > 200 ? 200 : movecost ) * ( trigdist && diagonal ? M_SQRT2 : 1 );
you.inv.rust_iron_items();
if( !you.is_mounted() ) {
you.burn_move_stamina( movecost );
}
body_part_set drenchFlags{ {
bp_leg_l, bp_leg_r, bp_torso, bp_arm_l,
bp_arm_r, bp_foot_l, bp_foot_r, bp_hand_l, bp_hand_r
}
};
if( you.is_underwater() ) {
drenchFlags |= { { bp_head, bp_eyes, bp_mouth, bp_hand_l, bp_hand_r } };
}
you.drench( 100, drenchFlags, true );
}
static float rate_critter( const Creature &c )
{
const npc *np = dynamic_cast<const npc *>( &c );
if( np != nullptr ) {
return np->weapon_value( np->weapon );
}
const monster *m = dynamic_cast<const monster *>( &c );
return m->type->difficulty;
}
void avatar_action::autoattack( avatar &you, map &m )
{
int reach = you.weapon.reach_range( you );
std::vector<Creature *> critters = you.get_targetable_creatures( reach );
critters.erase( std::remove_if( critters.begin(), critters.end(), []( const Creature * c ) {
if( !c->is_npc() ) {
return false;
}
return !dynamic_cast<const npc *>( c )->is_enemy();
} ), critters.end() );
if( critters.empty() ) {
add_msg( m_info, _( "No hostile creature in reach. Waiting a turn." ) );
if( g->check_safe_mode_allowed() ) {
you.pause();
}
return;
}
Creature &best = **std::max_element( critters.begin(), critters.end(),
[]( const Creature * l, const Creature * r ) {
return rate_critter( *l ) > rate_critter( *r );
} );
const tripoint diff = best.pos() - you.pos();
if( abs( diff.x ) <= 1 && abs( diff.y ) <= 1 && diff.z == 0 ) {
move( you, m, tripoint( diff.xy(), 0 ) );
return;
}
you.reach_attack( best.pos() );
}
/**
* Common checks for gunmode (when firing a weapon / manually firing turret)
* @param messages Used to store messages describing failed checks
* @return True if all conditions are true
*/
static bool gunmode_checks_common( avatar &you, const map &m, std::vector<std::string> &messages,
const gun_mode &gmode )
{
bool result = true;
// Check that passed gun mode is valid and we are able to use it
if( !( gmode && you.can_use( *gmode ) ) ) {
messages.push_back( string_format( _( "You can't currently fire your %s." ),
gmode->tname() ) );
result = false;
}
const optional_vpart_position vp = m.veh_at( you.pos() );
if( vp && vp->vehicle().player_in_control( you ) && ( gmode->is_two_handed( you ) ||
gmode->has_flag( flag_FIRE_TWOHAND ) ) ) {
messages.push_back( string_format( _( "You can't fire your %s while driving." ),
gmode->tname() ) );
result = false;
}
if( gmode->has_flag( flag_FIRE_TWOHAND ) && ( !you.has_two_arms() ||
you.worn_with_flag( flag_RESTRICT_HANDS ) ) ) {
messages.push_back( string_format( _( "You need two free hands to fire your %s." ),
gmode->tname() ) );
result = false;
}
return result;
}
/**
* Various checks for gunmode when firing a weapon
* @param messages Used to store messages describing failed checks
* @return True if all conditions are true
*/
static bool gunmode_checks_weapon( avatar &you, const map &m, std::vector<std::string> &messages,
const gun_mode &gmode )
{
bool result = true;
if( !gmode->ammo_sufficient() && !gmode->has_flag( flag_RELOAD_AND_SHOOT ) ) {
if( !gmode->ammo_remaining() ) {
messages.push_back( string_format( _( "Your %s is empty!" ), gmode->tname() ) );
} else {
messages.push_back( string_format( _( "Your %s needs %i charges to fire!" ),
gmode->tname(), gmode->ammo_required() ) );
}
result = false;
}
if( gmode->get_gun_ups_drain() > 0 ) {
const int ups_drain = gmode->get_gun_ups_drain();
const int adv_ups_drain = std::max( 1, ups_drain * 3 / 5 );
bool is_mech_weapon = false;
if( you.is_mounted() ) {
monster *mons = g->u.mounted_creature.get();
if( !mons->type->mech_weapon.empty() ) {
is_mech_weapon = true;
}
}
if( !is_mech_weapon ) {
if( !( you.has_charges( "UPS_off", ups_drain ) ||
you.has_charges( "adv_UPS_off", adv_ups_drain ) ||
( you.has_active_bionic( bio_ups ) &&
you.get_power_level() >= units::from_kilojoule( ups_drain ) ) ) ) {
messages.push_back( string_format(
_( "You need a UPS with at least %2$d charges or an advanced UPS with at least %3$d charges to fire the %1$s!" ),
gmode->tname(), ups_drain, adv_ups_drain ) );
result = false;
}
} else {
if( !you.has_charges( "UPS", ups_drain ) ) {
messages.push_back( string_format( _( "Your mech has an empty battery, its %s will not fire." ),
gmode->tname() ) );
result = false;
}
}
}
if( gmode->has_flag( flag_MOUNTED_GUN ) ) {
const bool v_mountable = static_cast<bool>( m.veh_at( you.pos() ).part_with_feature( "MOUNTABLE",
true ) );
bool t_mountable = m.has_flag_ter_or_furn( flag_MOUNTABLE, you.pos() );
if( !t_mountable && !v_mountable ) {
messages.push_back( string_format(
_( "You must stand near acceptable terrain or furniture to fire the %s. A table, a mound of dirt, a broken window, etc." ),
gmode->tname() ) );
result = false;
}
}
return result;
}
// TODO: Move data/functions related to targeting out of game class
/**
* Checks if the weapon is valid and if the player meets certain conditions for firing it.
* @return True if all conditions are true, otherwise false.
*/
static bool can_fire_weapon( avatar &you, const map &m, const item &weapon )
{
if( !weapon.is_gun() ) {
debugmsg( "Expected item to be a gun" );
return false;
}
if( you.has_effect( effect_relax_gas ) ) {
if( one_in( 5 ) ) {
add_msg( m_good, _( "Your eyes steel, and you raise your weapon!" ) );
} else {
you.moves -= rng( 2, 5 ) * 10;
add_msg( m_bad, _( "You can't fire your weapon, it's too heavy…" ) );
return false;
}
}
std::vector<std::string> messages;
for( const std::pair<const gun_mode_id, gun_mode> &mode_map : weapon.gun_all_modes() ) {
bool check_common = gunmode_checks_common( you, m, messages, mode_map.second );
bool check_weapon = gunmode_checks_weapon( you, m, messages, mode_map.second );
bool can_use_mode = check_common && check_weapon;
if( can_use_mode ) {
return true;
}
}
for( const std::string &message : messages ) {
add_msg( m_info, message );
}
return false;
}
/**
* Checks if the turret is valid and if the player meets certain conditions for manually firing it.
* @param tdata Turret to check.
* @return True if all conditions are true, otherwise false.
*/
static bool can_fire_turret( avatar &you, const map &m, const turret_data &turret )
{
const item &weapon = *turret.base();
if( !weapon.is_gun() ) {
debugmsg( "Expected turret base to be a gun." );
return false;
}
switch( turret.query() ) {
case turret_data::status::no_ammo:
add_msg( m_bad, _( "The %s is out of ammo." ), turret.name() );
return false;
case turret_data::status::no_power:
add_msg( m_bad, _( "The %s is not powered." ), turret.name() );
return false;
case turret_data::status::ready:
break;
default:
debugmsg( "Unknown turret status" );
return false;
}
if( you.has_effect( effect_relax_gas ) ) {
if( one_in( 5 ) ) {
add_msg( m_good, _( "Your eyes steel, and you aim your weapon!" ) );
} else {
you.moves -= rng( 2, 5 ) * 10;
add_msg( m_bad, _( "You are too pacified to aim the turret…" ) );
return false;
}
}
std::vector<std::string> messages;
for( const std::pair<const gun_mode_id, gun_mode> &mode_map : weapon.gun_all_modes() ) {
bool can_use_mode = gunmode_checks_common( you, m, messages, mode_map.second );
if( can_use_mode ) {
return true;
}
}
for( const std::string &message : messages ) {
add_msg( m_info, message );
}
return false;
}
void avatar_action::aim_do_turn( avatar &you, map &m )
{
targeting_data &args = you.get_targeting_data();
item *weapon = nullptr;
switch( args.weapon_source ) {
case WEAPON_SOURCE_WIELDED:
// TODO: if wielding a gun, check that this is the same gun that was used to start aiming
if( !you.weapon.is_null() ) {
// Gun wasn't lost (e.g. yanked by zombie technician)
weapon = &you.weapon;
}
break;
case WEAPON_SOURCE_BIONIC:
case WEAPON_SOURCE_MUTATION:
// TODO: this should check if the player lost relevant bionic/mutation
weapon = args.cached_fake_weapon.get();
break;
case WEAPON_SOURCE_INVALID:
case NUM_WEAPON_SOURCES:
debugmsg( "Expected valid targeting data" );
break;
}
if( !weapon || !can_fire_weapon( you, m, *weapon ) ) {
you.cancel_activity();
return;
}
int reload_time = 0;
gun_mode gun = weapon->gun_current_mode();
// TODO: use MODERATE_EXERCISE if firing a bow
you.increase_activity_level( LIGHT_EXERCISE );
// TODO: move handling "RELOAD_AND_SHOOT" flagged guns to a separate function.
if( gun->has_flag( flag_RELOAD_AND_SHOOT ) ) {
if( !gun->ammo_remaining() ) {
const auto ammo_location_is_valid = [&]() -> bool {
if( !you.ammo_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 ) : you.select_ammo( *gun );
if( !opt ) {
// Menu canceled
return;
}
reload_time += opt.moves();
if( !gun->reload( you, std::move( opt.ammo ), 1 ) ) {
// Reload not allowed
return;
}
// Burn 0.2% max base stamina x the strength required to fire.
you.mod_stamina( gun->get_min_str() * static_cast<int>( 0.002f *
get_option<int>( "PLAYER_MAX_STAMINA" ) ) );
// 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;
g->refresh_all();
}
}
int range = gun.target->gun_range( &you );
const itype *ammo = gun->ammo_data();
g->temp_exit_fullscreen();
m.draw( g->w_terrain, you.pos() );
std::vector<tripoint> trajectory = target_handler().target_ui( you, TARGET_MODE_FIRE, weapon, range,
ammo );
//may be changed in target_ui
gun = weapon->gun_current_mode();
if( trajectory.empty() ) {
bool not_aiming = you.activity.id() != ACT_AIM;
if( not_aiming && gun->has_flag( flag_RELOAD_AND_SHOOT ) ) {
const auto previous_moves = you.moves;
g->unload( *gun );
// Give back time for unloading as essentially nothing has been done.
// Note that reload_time has not been applied either.
you.moves = previous_moves;
}
g->reenter_fullscreen();
return;
}
// Recenter our view
g->draw_ter();
wrefresh( g->w_terrain );
g->draw_panels();
you.moves -= reload_time;
// TODO: add check for TRIGGERHAPPY
int shots_fired = you.fire_gun( trajectory.back(), gun.qty, *gun );
// TODO: bionic power cost of firing should be derived from a value of the relevant weapon.
if( shots_fired && ( args.bp_cost_per_shot > 0_J ) ) {
you.mod_power_level( -args.bp_cost_per_shot * shots_fired );
}
g->reenter_fullscreen();
}
void avatar_action::fire_wielded_weapon( avatar &you, map &m )
{
item &weapon = you.weapon;
if( weapon.is_gunmod() ) {
add_msg( m_info,
_( "The %s must be attached to a gun, it can not be fired separately." ),
weapon.tname() );
return;
} else if( !weapon.is_gun() ) {
return;
} else if( weapon.ammo_data() && !weapon.ammo_types().count( weapon.ammo_data()->ammo->type ) ) {
add_msg( m_info, _( "The %s can't be fired while loaded with incompatible ammunition %s" ),
weapon.tname(), weapon.ammo_current() );
return;
}
targeting_data args = targeting_data::use_wielded();
you.set_targeting_data( args );
avatar_action::aim_do_turn( you, m );
}
void avatar_action::fire_ranged_mutation( avatar &you, map &m, const item &fake_gun )
{
targeting_data args = targeting_data::use_mutation( fake_gun );
you.set_targeting_data( args );
avatar_action::aim_do_turn( you, m );
}
void avatar_action::fire_ranged_bionic( avatar &you, map &m, const item &fake_gun,
units::energy cost_per_shot )
{
targeting_data args = targeting_data::use_bionic( fake_gun, cost_per_shot );
you.set_targeting_data( args );
avatar_action::aim_do_turn( you, m );
}
void avatar_action::fire_turret_manual( avatar &you, map &m, turret_data &turret )
{
if( !can_fire_turret( you, m, turret ) ) {
return;
}
item *turret_base = &*turret.base();
g->temp_exit_fullscreen();
g->m.draw( g->w_terrain, you.pos() );
std::vector<tripoint> trajectory = target_handler().target_ui(
you,
TARGET_MODE_TURRET_MANUAL,
turret_base,
turret.range(),
turret.ammo_data(),
&turret
);
if( !trajectory.empty() ) {
// Recenter our view
g->draw_ter();
wrefresh( g->w_terrain );
g->draw_panels();
// TODO: add check for TRIGGERHAPPY
turret.fire( you, trajectory.back() );
}
g->reenter_fullscreen();
}
void avatar_action::mend( avatar &you, item_location loc )