forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
creature.cpp
2054 lines (1848 loc) · 63.8 KB
/
creature.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 "creature.h"
#include <algorithm>
#include <array>
#include <cstdlib>
#include <map>
#include <memory>
#include <optional>
#include "anatomy.h"
#include "avatar.h"
#include "calendar.h"
#include "character.h"
#include "color.h"
#include "cursesdef.h"
#include "damage.h"
#include "debug.h"
#include "effect.h"
#include "enums.h"
#include "event.h"
#include "event_bus.h"
#include "field.h"
#include "game.h"
#include "game_constants.h"
#include "int_id.h"
#include "item.h"
#include "json.h"
#include "lightmap.h"
#include "line.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "messages.h"
#include "monster.h"
#include "mtype.h"
#include "npc.h"
#include "output.h"
#include "player.h"
#include "point.h"
#include "projectile.h"
#include "ranged.h"
#include "rng.h"
#include "string_id.h"
#include "string_utils.h"
#include "translations.h"
#include "value_ptr.h"
#include "vehicle.h"
#include "vpart_position.h"
static const ammo_effect_str_id ammo_effect_APPLY_SAP( "APPLY_SAP" );
static const ammo_effect_str_id ammo_effect_BEANBAG( "BEANBAG" );
static const ammo_effect_str_id ammo_effect_BLINDS_EYES( "BLINDS_EYES" );
static const ammo_effect_str_id ammo_effect_BOUNCE( "BOUNCE" );
static const ammo_effect_str_id ammo_effect_IGNITE( "IGNITE" );
static const ammo_effect_str_id ammo_effect_INCENDIARY( "INCENDIARY" );
static const ammo_effect_str_id ammo_effect_LARGE_BEANBAG( "LARGE_BEANBAG" );
static const ammo_effect_str_id ammo_effect_magic( "magic" );
static const ammo_effect_str_id ammo_effect_NO_CRIT( "NO_CRIT" );
static const ammo_effect_str_id ammo_effect_NO_DAMAGE_SCALING( "NO_DAMAGE_SCALING" );
static const ammo_effect_str_id ammo_effect_NOGIB( "NOGIB" );
static const ammo_effect_str_id ammo_effect_PARALYZEPOISON( "PARALYZEPOISON" );
static const ammo_effect_str_id ammo_effect_TANGLE( "TANGLE" );
static const efftype_id effect_blind( "blind" );
static const efftype_id effect_bounced( "bounced" );
static const efftype_id effect_downed( "downed" );
static const efftype_id effect_lying_down( "lying_down" );
static const efftype_id effect_no_sight( "no_sight" );
static const efftype_id effect_npc_suspend( "npc_suspend" );
static const efftype_id effect_onfire( "onfire" );
static const efftype_id effect_paralyzepoison( "paralyzepoison" );
static const efftype_id effect_ridden( "ridden" );
static const efftype_id effect_riding( "riding" );
static const efftype_id effect_sap( "sap" );
static const efftype_id effect_sleep( "sleep" );
static const efftype_id effect_stunned( "stunned" );
static const efftype_id effect_tied( "tied" );
static const efftype_id effect_zapped( "zapped" );
const std::map<std::string, m_size> Creature::size_map = {
{"TINY", MS_TINY}, {"SMALL", MS_SMALL}, {"MEDIUM", MS_MEDIUM},
{"LARGE", MS_LARGE}, {"HUGE", MS_HUGE}
};
const std::set<material_id> Creature::cmat_flesh{
material_id( "flesh" ), material_id( "iflesh" )
};
const std::set<material_id> Creature::cmat_fleshnveg{
material_id( "flesh" ), material_id( "iflesh" ), material_id( "veggy" )
};
const std::set<material_id> Creature::cmat_flammable{
material_id( "paper" ), material_id( "powder" ), material_id( "wood" ),
material_id( "cotton" ), material_id( "wool" )
};
const std::set<material_id> Creature::cmat_flameres{
material_id( "stone" ), material_id( "kevlar" ), material_id( "steel" )
};
Creature::Creature()
{
moves = 0;
pain = 0;
killer = nullptr;
speed_base = 100;
underwater = false;
Creature::reset_bonuses();
fake = false;
}
Creature::~Creature() = default;
std::vector<std::string> Creature::get_grammatical_genders() const
{
// Returning empty list means we use the language-specified default
return {};
}
void Creature::reset()
{
reset_bonuses();
reset_stats();
}
void Creature::bleed() const
{
const field_type_id &blood_type = bloodType();
if( blood_type ) {
get_map().add_splatter( blood_type, pos() );
}
}
void Creature::reset_bonuses()
{
num_blocks = 1;
num_dodges = 1;
num_blocks_bonus = 0;
num_dodges_bonus = 0;
armor_bash_bonus = 0;
armor_cut_bonus = 0;
armor_bullet_bonus = 0;
speed_bonus = 0;
speed_mult = 0;
dodge_bonus = 0;
block_bonus = 0;
hit_bonus = 0;
}
void Creature::process_turn()
{
if( is_dead_state() ) {
return;
}
reset_bonuses();
process_effects();
// Call this in case any effects have changed our stats
reset_stats();
// add an appropriate number of moves
if( !has_effect( effect_ridden ) ) {
moves += get_speed();
}
}
bool Creature::is_underwater() const
{
return underwater;
}
void Creature::set_underwater( bool x )
{
underwater = x;
}
bool Creature::digging() const
{
return false;
}
bool Creature::is_dangerous_fields( const field &fld ) const
{
// Else check each field to see if it's dangerous to us
for( auto &dfield : fld ) {
if( is_dangerous_field( dfield.second ) ) {
return true;
}
}
// No fields were found to be dangerous, so the field set isn't dangerous
return false;
}
bool Creature::is_dangerous_field( const field_entry &entry ) const
{
// If it's dangerous and we're not immune return true, else return false
return entry.is_dangerous() && !is_immune_field( entry.get_field_type() );
}
bool Creature::sees( const Creature &critter ) const
{
// Creatures always see themselves (simplifies drawing).
if( &critter == this ) {
return true;
}
if( critter.is_hallucination() ) {
// hallucinations are imaginations of the player character, npcs or monsters don't hallucinate.
// Invisible hallucinations would be pretty useless (nobody would see them at all), therefor
// the player will see them always.
return is_player();
}
if( !fov_3d && !debug_mode && posz() != critter.posz() ) {
return false;
}
// This check is ridiculously expensive so defer it to after everything else.
auto visible = []( const Character * ch ) {
return ch == nullptr || !ch->is_invisible();
};
map &here = get_map();
const Character *ch = critter.as_character();
const int wanted_range = rl_dist( pos(), critter.pos() );
// Can always see adjacent monsters on the same level, unless they're through a vehicle wall.
// We also bypass lighting for vertically adjacent monsters, but still check for floors.
if( wanted_range <= 1 && ( posz() == critter.posz() || here.sees( pos(), critter.pos(), 1 ) ) ) {
if( here.obscured_by_vehicle_rotation( pos(), critter.pos() ) ) {
return false;
}
return visible( ch );
} else if( ( wanted_range > 1 && critter.digging() ) ||
( critter.has_flag( MF_NIGHT_INVISIBILITY ) && here.light_at( critter.pos() ) <= lit_level::LOW ) ||
( critter.is_underwater() && !is_underwater() && here.is_divable( critter.pos() ) ) ||
( here.has_flag_ter_or_furn( TFLAG_HIDE_PLACE, critter.pos() ) &&
!( std::abs( posx() - critter.posx() ) <= 1 && std::abs( posy() - critter.posy() ) <= 1 &&
std::abs( posz() - critter.posz() ) <= 1 ) ) ) {
return false;
}
if( ch != nullptr ) {
if( ch->movement_mode_is( CMM_CROUCH ) ) {
const int coverage = here.obstacle_coverage( pos(), critter.pos() );
if( coverage < 30 ) {
return sees( critter.pos(), critter.is_avatar() ) && visible( ch );
}
float size_modifier = 1.0;
switch( ch->get_size() ) {
case MS_TINY:
size_modifier = 2.0;
break;
case MS_SMALL:
size_modifier = 1.4;
break;
case MS_MEDIUM:
break;
case MS_LARGE:
size_modifier = 0.6;
break;
case MS_HUGE:
size_modifier = 0.15;
break;
default:
break;
}
const int vision_modifier = 30 - 0.5 * coverage * size_modifier;
if( vision_modifier > 1 ) {
return sees( critter.pos(), critter.is_avatar(), vision_modifier ) && visible( ch );
}
return false;
}
}
return sees( critter.pos(), critter.is_avatar() ) && visible( ch );
}
bool Creature::sees( const tripoint &t, bool is_avatar, int range_mod ) const
{
if( !fov_3d && posz() != t.z ) {
return false;
}
map &here = get_map();
const int range_cur = sight_range( here.ambient_light_at( t ) );
const int range_day = sight_range( default_daylight_level() );
const int range_night = sight_range( 0 );
const int range_max = std::max( range_day, range_night );
const int range_min = std::min( range_cur, range_max );
const int wanted_range = rl_dist( pos(), t );
if( wanted_range <= range_min ||
( wanted_range <= range_max &&
here.ambient_light_at( t ) > g->natural_light_level( t.z ) ) ) {
int range = 0;
if( here.ambient_light_at( t ) > g->natural_light_level( t.z ) ) {
range = MAX_VIEW_DISTANCE;
} else {
range = range_min;
}
if( has_effect( effect_no_sight ) ) {
range = 1;
}
if( range_mod > 0 ) {
range = std::min( range, range_mod );
}
if( is_avatar ) {
// Special case monster -> player visibility, forcing it to be symmetric with player vision.
const float player_visibility_factor = g->u.visibility() / 100.0f;
int adj_range = std::floor( range * player_visibility_factor );
return adj_range >= wanted_range &&
here.get_cache_ref( pos().z ).seen_cache[pos().x][pos().y] > LIGHT_TRANSPARENCY_SOLID;
} else {
return here.sees( pos(), t, range );
}
} else {
return false;
}
}
// Helper function to check if potential area of effect of a weapon overlaps vehicle
// Maybe TODO: If this is too slow, precalculate a bounding box and clip the tested area to it
static bool overlaps_vehicle( const std::set<tripoint> &veh_area, const tripoint &pos,
const int area )
{
for( const tripoint &tmp : tripoint_range<tripoint>( pos - tripoint( area, area, 0 ),
pos + tripoint( area - 1, area - 1, 0 ) ) ) {
if( veh_area.count( tmp ) > 0 ) {
return true;
}
}
return false;
}
Creature *Creature::auto_find_hostile_target( int range, int &boo_hoo, int area )
{
Creature *target = nullptr;
player &u = g->u; // Could easily protect something that isn't the player
constexpr int hostile_adj = 2; // Priority bonus for hostile targets
const int iff_dist = ( range + area ) * 3 / 2 + 6; // iff check triggers at this distance
// iff safety margin (degrees). less accuracy, more paranoia
units::angle iff_hangle = units::from_degrees( 15 + area );
float best_target_rating = -1.0f; // bigger is better
units::angle u_angle = {}; // player angle relative to turret
boo_hoo = 0; // how many targets were passed due to IFF. Tragically.
bool self_area_iff = false; // Need to check if the target is near the vehicle we're a part of
bool area_iff = false; // Need to check distance from target to player
bool angle_iff = true; // Need to check if player is in a cone between us and target
int pldist = rl_dist( pos(), g->u.pos() );
map &here = get_map();
vehicle *in_veh = is_fake() ? veh_pointer_or_null( here.veh_at( pos() ) ) : nullptr;
if( pldist < iff_dist && sees( g->u ) ) {
area_iff = area > 0;
angle_iff = true;
// Player inside vehicle won't be hit by shots from the roof,
// so we can fire "through" them just fine.
const optional_vpart_position vp = here.veh_at( u.pos() );
if( in_veh && veh_pointer_or_null( vp ) == in_veh && vp->is_inside() ) {
angle_iff = false; // No angle IFF, but possibly area IFF
} else if( pldist < 3 ) {
// granularity increases with proximity
iff_hangle = ( pldist == 2 ? 30_degrees : 60_degrees );
}
u_angle = coord_to_angle( pos(), u.pos() );
}
if( area > 0 && in_veh != nullptr ) {
self_area_iff = true;
}
std::vector<Creature *> targets = g->get_creatures_if( [&]( const Creature & critter ) {
if( critter.is_monster() ) {
// friendly to the player, not a target for us
return static_cast<const monster *>( &critter )->friendly == 0;
}
if( critter.is_npc() ) {
// friendly to the player, not a target for us
return static_cast<const npc *>( &critter )->get_attitude() == NPCATT_KILL;
}
// TODO: what about g->u?
return false;
} );
for( auto &m : targets ) {
if( !sees( *m ) ) {
// can't see nor sense it
if( is_fake() && in_veh ) {
// If turret in the vehicle then
// Hack: trying yo avoid turret LOS blocking by frames bug by trying to see target from vehicle boundary
// Or turret wallhack for turret's car
// TODO: to visibility checking another way, probably using 3D FOV
std::vector<tripoint> path_to_target = line_to( pos(), m->pos() );
path_to_target.insert( path_to_target.begin(), pos() );
// Getting point on vehicle boundaries and on line between target and turret
bool continueFlag = true;
do {
const optional_vpart_position vp = here.veh_at( path_to_target.back() );
vehicle *const veh = vp ? &vp->vehicle() : nullptr;
if( in_veh == veh ) {
continueFlag = false;
} else {
path_to_target.pop_back();
}
} while( continueFlag );
tripoint oldPos = pos();
setpos( path_to_target.back() ); //Temporary moving targeting npc on vehicle boundary postion
bool seesFromVehBound = sees( *m ); // And look from there
setpos( oldPos );
if( !seesFromVehBound ) {
continue;
}
} else {
continue;
}
}
int dist = rl_dist( pos(), m->pos() ) + 1; // rl_dist can be 0
if( dist > range + 1 || dist < area ) {
// Too near or too far
continue;
}
// Prioritize big, armed and hostile stuff
float mon_rating = m->power_rating();
float target_rating = mon_rating / dist;
if( mon_rating + hostile_adj <= 0 ) {
// We wouldn't attack it even if it was hostile
continue;
}
if( in_veh != nullptr && veh_pointer_or_null( here.veh_at( m->pos() ) ) == in_veh ) {
// No shooting stuff on vehicle we're a part of
continue;
}
if( area_iff && rl_dist( u.pos(), m->pos() ) <= area ) {
// Player in AoE
boo_hoo++;
continue;
}
// Hostility check can be expensive, but we need to inform the player of boo_hoo
// only when the target is actually "hostile enough"
bool maybe_boo = false;
if( angle_iff ) {
units::angle tangle = coord_to_angle( pos(), m->pos() );
units::angle diff = units::fabs( u_angle - tangle );
// Player is in the angle and not too far behind the target
if( ( diff + iff_hangle > 360_degrees || diff < iff_hangle ) &&
( dist * 3 / 2 + 6 > pldist ) ) {
maybe_boo = true;
}
}
if( !maybe_boo && ( ( mon_rating + hostile_adj ) / dist <= best_target_rating ) ) {
// "Would we skip the target even if it was hostile?"
// Helps avoid (possibly expensive) attitude calculation
continue;
}
if( m->attitude_to( u ) == A_HOSTILE ) {
target_rating = ( mon_rating + hostile_adj ) / dist;
if( maybe_boo ) {
boo_hoo++;
continue;
}
}
if( target_rating <= best_target_rating || target_rating <= 0 ) {
continue; // Handle this late so that boo_hoo++ can happen
}
// Expensive check for proximity to vehicle
if( self_area_iff && overlaps_vehicle( in_veh->get_points(), m->pos(), area ) ) {
continue;
}
target = m;
best_target_rating = target_rating;
}
return target;
}
/*
* Damage-related functions
*/
int Creature::size_melee_penalty() const
{
switch( get_size() ) {
case MS_TINY:
return 30;
case MS_SMALL:
return 15;
case MS_MEDIUM:
return 0;
case MS_LARGE:
return -10;
case MS_HUGE:
return -20;
default:
break;
}
debugmsg( "Invalid target size %d", get_size() );
return 0;
}
int Creature::deal_melee_attack( Creature *source, int hitroll )
{
int hit_spread = hitroll - dodge_roll() - size_melee_penalty();
if( has_flag( MF_IMMOBILE ) ) {
// Under normal circumstances, even a clumsy person would
// not miss a turret. It should, however, be possible to
// miss a smaller target, especially when wielding a
// clumsy weapon or when severely encumbered.
hit_spread += 40;
}
// If attacker missed call targets on_dodge event
if( hit_spread <= 0 && source != nullptr && !source->is_hallucination() ) {
on_dodge( source, source->get_melee() );
}
return hit_spread;
}
void Creature::deal_melee_hit( Creature *source, int hit_spread, bool critical_hit,
const damage_instance &dam, dealt_damage_instance &dealt_dam )
{
if( source == nullptr || source->is_hallucination() ) {
dealt_dam.bp_hit = anatomy_id( "human_anatomy" )->random_body_part()->token;
return;
}
// If carrying a rider, there is a chance the hits may hit rider instead.
// melee attack will start off as targeted at mount
if( has_effect( effect_ridden ) ) {
monster *mons = dynamic_cast<monster *>( this );
if( mons && mons->mounted_player ) {
if( !mons->has_flag( MF_MECH_DEFENSIVE ) &&
one_in( std::max( 2, mons->get_size() - mons->mounted_player->get_size() ) ) ) {
mons->mounted_player->deal_melee_hit( source, hit_spread, critical_hit, dam, dealt_dam );
return;
}
}
}
damage_instance d = dam; // copy, since we will mutate in block_hit
bodypart_id bp_hit = convert_bp( select_body_part( source, hit_spread ) ).id();
const body_part bp_token = bp_hit->token;
block_hit( source, bp_hit, d );
on_hit( source, bp_hit ); // trigger on-gethit events
dealt_dam = deal_damage( source, bp_hit, d );
dealt_dam.bp_hit = bp_token;
}
namespace ranged
{
void print_dmg_msg( Creature &target, Creature *source, const dealt_damage_instance &dealt_dam,
hit_tier ht )
{
std::string message;
game_message_type sct_color = m_neutral;
switch( ht ) {
case hit_tier::grazing:
message = _( "Grazing hit." );
sct_color = m_grazing;
case hit_tier::normal:
break;
case hit_tier::critical:
message = _( "Critical!" );
sct_color = m_critical;
break;
}
if( source != nullptr && !message.empty() ) {
source->add_msg_if_player( m_good, message );
}
if( dealt_dam.total_damage() == 0 ) {
//~ 1$ - monster name, 2$ - character's bodypart or monster's skin/armor
add_msg( _( "The shot reflects off %1$s %2$s!" ), target.disp_name( true ),
( target.is_monster() || dealt_dam.bp_hit == num_bp ) ?
target.skin_name() :
body_part_name_accusative( dealt_dam.bp_hit ) );
} else if( target.is_player() ) {
//monster hits player ranged
//~ Hit message. 1$s is bodypart name in accusative. 2$d is damage value.
target.add_msg_if_player( m_bad, _( "You were hit in the %1$s for %2$d damage." ),
body_part_name_accusative( dealt_dam.bp_hit ),
dealt_dam.total_damage() );
} else if( source != nullptr ) {
if( source->is_player() ) {
//player hits monster ranged
SCT.add( target.pos().xy(),
direction_from( point_zero, target.pos().xy() - source->pos().xy() ),
get_hp_bar( dealt_dam.total_damage(), target.get_hp_max(), true ).first,
m_good, message, sct_color );
if( target.get_hp() > 0 ) {
SCT.add( target.pos().xy(),
direction_from( point_zero, target.pos().xy() - source->pos().xy() ),
get_hp_bar( target.get_hp(), target.get_hp_max(), true ).first, m_good,
//~ "hit points", used in scrolling combat text
_( "hp" ), m_neutral, "hp" );
} else {
SCT.removeCreatureHP();
}
//~ %1$s: creature name, %2$d: damage value
add_msg( m_good, _( "You hit %1$s for %2$d damage." ),
target.disp_name(), dealt_dam.total_damage() );
} else {
//~ 1$ - shooter, 2$ - target
add_msg( _( "%1$s shoots %2$s." ),
source->disp_name(), target.disp_name() );
}
}
}
dealt_damage_instance hit_with_aoe( Creature &target, Creature *source, const damage_instance &di )
{
const auto all_body_parts = target.get_body();
float hit_size_sum = std::accumulate( all_body_parts.begin(), all_body_parts.end(), 0.0f,
[]( float acc, const std::pair<bodypart_str_id, bodypart> &pr ) {
return acc + pr.first->hit_size;
} );
dealt_damage_instance dealt_damage;
for( const std::pair<const bodypart_str_id, bodypart> &pr : all_body_parts ) {
damage_instance impact = di;
impact.mult_damage( pr.first->hit_size / hit_size_sum );
dealt_damage_instance bp_damage = target.deal_damage( source, pr.first.id(), impact );
for( size_t i = 0; i < dealt_damage.dealt_dams.size(); i++ ) {
dealt_damage.dealt_dams[i] += bp_damage.dealt_dams[i];
}
}
dealt_damage.bp_hit = bodypart_str_id::NULL_ID()->token;
if( get_player_character().sees( target ) ) {
ranged::print_dmg_msg( target, source, dealt_damage );
}
if( target.has_effect( effect_ridden ) ) {
monster *mons = dynamic_cast<monster *>( &target );
if( mons && mons->mounted_player && !mons->has_flag( MF_MECH_DEFENSIVE ) ) {
// TODO: Return value
hit_with_aoe( *mons->mounted_player, source, di );
}
}
return dealt_damage;
}
} // namespace ranged
/**
* Attempts to harm a creature with a projectile.
*
* @param source Pointer to the creature who shot the projectile.
* @param attack A structure describing the attack and its results.
* @param print_messages enables message printing by default.
*/
void Creature::deal_projectile_attack( Creature *source, dealt_projectile_attack &attack )
{
const bool magic = attack.proj.has_effect( ammo_effect_magic );
const bool targetted_crit_allowed = !attack.proj.has_effect( ammo_effect_NO_CRIT );
const double missed_by = attack.missed_by;
if( missed_by >= 1.0 && !magic ) {
// Total miss
return;
}
// If carrying a rider, there is a chance the hits may hit rider instead.
if( has_effect( effect_ridden ) ) {
monster *mons = dynamic_cast<monster *>( this );
if( mons && mons->mounted_player ) {
if( !mons->has_flag( MF_MECH_DEFENSIVE ) &&
one_in( std::max( 2, mons->get_size() - mons->mounted_player->get_size() ) ) ) {
mons->mounted_player->deal_projectile_attack( source, attack );
return;
}
}
}
const projectile &proj = attack.proj;
dealt_damage_instance &dealt_dam = attack.dealt_dam;
const bool u_see_this = g->u.sees( *this );
const int avoid_roll = dodge_roll();
// Do dice(10, speed) instead of dice(speed, 10) because speed could potentially be > 10000
const int diff_roll = dice( 10, proj.speed );
// Partial dodge, capped at [0.0, 1.0], added to missed_by
const double dodge_rescaled = avoid_roll / static_cast<double>( diff_roll );
const double goodhit = missed_by + std::max( 0.0, std::min( 1.0, dodge_rescaled ) );
if( goodhit >= 1.0 && !magic ) {
attack.missed_by = 1.0; // Arbitrary value
// "Avoid" rather than "dodge", because it includes removing self from the line of fire
// rather than just Matrix-style bullet dodging
if( source != nullptr && g->u.sees( *source ) ) {
add_msg_player_or_npc(
m_warning,
_( "You avoid %s projectile!" ),
_( "<npcname> avoids %s projectile." ),
source->disp_name( true ) );
} else {
add_msg_player_or_npc(
m_warning,
_( "You avoid an incoming projectile!" ),
_( "<npcname> avoids an incoming projectile." ) );
}
return;
}
// Bounce applies whether it does damage or not.
if( proj.has_effect( ammo_effect_BOUNCE ) ) {
add_effect( effect_bounced, 1_turns );
}
bodypart_id bp_hit;
double hit_value = missed_by + rng_float( -0.5, 0.5 );
if( targetted_crit_allowed || magic ) { //default logic for selecting bodypart
if( goodhit < accuracy_critical && hit_value <= 0.2 ) {
bp_hit = bodypart_str_id( "head" );
} else if( hit_value <= 0.4 || magic ) {
bp_hit = bodypart_str_id( "torso" );
} else if( one_in( 4 ) ) {
if( one_in( 2 ) ) {
bp_hit = bodypart_str_id( "leg_l" );
} else {
bp_hit = bodypart_str_id( "leg_r" );
}
} else {
if( one_in( 2 ) ) {
bp_hit = bodypart_str_id( "arm_l" );
} else {
bp_hit = bodypart_str_id( "arm_r" );
}
}
} else { // no crit logic for selecting bodypart
if( hit_value <= 0.4 && !one_in( 4 ) ) {
bp_hit = one_in( 3 ) ? bodypart_str_id( "head" ) : bodypart_str_id( "torso" );
} else if( one_in( 4 ) ) {
if( one_in( 2 ) ) {
bp_hit = bodypart_str_id( "leg_l" );
} else {
bp_hit = bodypart_str_id( "leg_r" );
}
} else {
if( one_in( 2 ) ) {
bp_hit = bodypart_str_id( "arm_l" );
} else {
bp_hit = bodypart_str_id( "arm_r" );
}
}
}
double damage_mult = 1.0;
ranged::hit_tier ht = ranged::hit_tier::normal;
if( magic ) {
damage_mult *= rng_float( 0.9, 1.1 );
} else if( targetted_crit_allowed && goodhit < accuracy_critical ) {
ht = ranged::hit_tier::critical;
damage_mult *= 1.5;
} else if( goodhit < accuracy_standard ) {
damage_mult *= rng_float( 0.9, 1.1 );
} else if( goodhit < accuracy_grazing ) {
ht = ranged::hit_tier::grazing;
damage_mult *= 0.5;
}
attack.missed_by = goodhit;
// copy it, since we're mutating
damage_instance impact = proj.impact;
if( damage_mult > 0.0f && proj.has_effect( ammo_effect_NO_DAMAGE_SCALING ) ) {
damage_mult = 1.0f;
}
impact.mult_damage( damage_mult );
if( proj.has_effect( ammo_effect_NOGIB ) ) {
float dmg_ratio = static_cast<float>( impact.total_damage() ) / get_hp_max( bp_hit );
if( dmg_ratio > 1.25f ) {
impact.mult_damage( 1.0f / dmg_ratio );
}
}
// If we have a shield, it might passively block ranged impacts
block_ranged_hit( source, bp_hit, impact );
dealt_dam = deal_damage( source, bp_hit, impact );
dealt_dam.bp_hit = bp_hit->token;
// Apply ammo effects to target.
if( proj.has_effect( ammo_effect_TANGLE ) ) {
monster *z = dynamic_cast<monster *>( this );
player *n = dynamic_cast<player *>( this );
// if its a tameable animal, its a good way to catch them if they are running away, like them ranchers do!
// we assume immediate success, then certain monster types immediately break free in monster.cpp move_effects()
if( z ) {
const item &drop_item = proj.get_drop();
if( !drop_item.is_null() ) {
z->add_effect( effect_tied, 1_turns, num_bp );
z->tied_item = cata::make_value<item>( drop_item );
} else {
add_msg( m_debug, "projectile with TANGLE effect, but no drop item specified" );
}
} else if( n && !is_immune_effect( effect_downed ) ) {
// no tied up effect for people yet, just down them and stun them, its close enough to the desired effect.
// we can assume a person knows how to untangle their legs eventually and not panic like an animal.
add_effect( effect_downed, 1_turns );
// stunned to simulate staggering around and stumbling trying to get the entangled thing off of them.
add_effect( effect_stunned, rng( 3_turns, 8_turns ) );
}
}
if( proj.has_effect( ammo_effect_INCENDIARY ) ) {
if( made_of( material_id( "veggy" ) ) || made_of_any( cmat_flammable ) ) {
add_effect( effect_onfire, rng( 2_turns, 6_turns ), bp_hit->token );
} else if( made_of_any( cmat_flesh ) && one_in( 4 ) ) {
add_effect( effect_onfire, rng( 1_turns, 4_turns ), bp_hit->token );
}
} else if( proj.has_effect( ammo_effect_IGNITE ) ) {
if( made_of( material_id( "veggy" ) ) || made_of_any( cmat_flammable ) ) {
add_effect( effect_onfire, 6_turns, bp_hit->token );
} else if( made_of_any( cmat_flesh ) ) {
add_effect( effect_onfire, 10_turns, bp_hit->token );
}
}
if( bp_hit == bodypart_str_id( "head" ) && proj.has_effect( ammo_effect_BLINDS_EYES ) ) {
// TODO: Change this to require bp_eyes
add_env_effect( effect_blind, bp_eyes, 5, rng( 3_turns, 10_turns ) );
}
if( proj.has_effect( ammo_effect_APPLY_SAP ) ) {
add_effect( effect_sap, 1_turns * dealt_dam.total_damage() );
}
if( proj.has_effect( ammo_effect_PARALYZEPOISON ) && dealt_dam.total_damage() > 0 ) {
add_msg_if_player( m_bad, _( "You feel poison coursing through your body!" ) );
add_effect( effect_paralyzepoison, 5_minutes );
}
int stun_strength = 0;
if( proj.has_effect( ammo_effect_BEANBAG ) ) {
stun_strength = 4;
}
if( proj.has_effect( ammo_effect_LARGE_BEANBAG ) ) {
stun_strength = 16;
}
if( stun_strength > 0 ) {
switch( get_size() ) {
case MS_TINY:
stun_strength *= 4;
break;
case MS_SMALL:
stun_strength *= 2;
break;
case MS_MEDIUM:
default:
break;
case MS_LARGE:
stun_strength /= 2;
break;
case MS_HUGE:
stun_strength /= 4;
break;
}
add_effect( effect_stunned, 1_turns * rng( stun_strength / 2, stun_strength ) );
}
if( u_see_this ) {
if( damage_mult == 0 ) {
if( source != nullptr ) {
add_msg( source->is_player() ? _( "You miss!" ) : _( "The shot misses!" ) );
}
} else {
ranged::print_dmg_msg( *this, source, dealt_dam, ht );
}
}
check_dead_state();
attack.hit_critter = this;
attack.missed_by = goodhit;
}
dealt_damage_instance Creature::deal_damage( Creature *source, bodypart_id bp,
const damage_instance &dam )
{
if( is_dead_state() ) {
return dealt_damage_instance();
}
int total_damage = 0;
int total_pain = 0;
damage_instance d = dam; // copy, since we will mutate in absorb_hit
dealt_damage_instance dealt_dams;
absorb_hit( bp, d );
// Add up all the damage units dealt
for( const auto &it : d.damage_units ) {
int cur_damage = 0;
deal_damage_handle_type( it, bp, cur_damage, total_pain );
if( cur_damage > 0 ) {
dealt_dams.dealt_dams[ it.type ] += cur_damage;
total_damage += cur_damage;
}
}
mod_pain( total_pain );
apply_damage( source, bp, total_damage );
return dealt_dams;
}
void Creature::deal_damage_handle_type( const damage_unit &du, bodypart_id bp, int &damage,
int &pain )
{
// Handles ACIDPROOF, electric immunity etc.
if( is_immune_damage( du.type ) ) {
return;
}
// Apply damage multiplier from skill, critical hits or grazes after all other modifications.
const int adjusted_damage = du.amount * du.damage_multiplier;
if( adjusted_damage <= 0 ) {
return;
}
float div = 4.0f;
switch( du.type ) {
case DT_BASH:
// Bashing damage is less painful
div = 5.0f;
break;
case DT_HEAT:
// heat damage sets us on fire sometimes
if( rng( 0, 100 ) < adjusted_damage ) {
add_effect( effect_onfire, rng( 1_turns, 3_turns ), bp->token );
}
break;
case DT_ELECTRIC:
// Electrical damage adds a major speed/dex debuff
add_effect( effect_zapped, 1_turns * std::max( adjusted_damage, 2 ) );
break;
case DT_ACID:
// Acid damage and acid burns are more painful
div = 3.0f;
break;
default:
break;
}
on_damage_of_type( adjusted_damage, du.type, bp );
damage += adjusted_damage;
pain += roll_remainder( adjusted_damage / div );
}
void Creature::on_dodge( Creature */*source*/, int /*difficulty*/ )
{
}
/*
* State check functions
*/
bool Creature::is_warm() const
{
return true;
}
bool Creature::in_species( const species_id & ) const
{
return false;
}
bool Creature::is_fake() const
{
return fake;
}
void Creature::set_fake( const bool fake_value )
{
fake = fake_value;
}
void Creature::add_effect( const effect &eff, bool force, bool deferred )
{
add_effect( eff.get_id(), eff.get_duration(), eff.get_bp(), eff.get_intensity(),
force, deferred );
}
void Creature::add_effect( const efftype_id &eff_id, const time_duration &dur, body_part bp,
int intensity, bool force, bool deferred )
{
add_effect( eff_id, dur, convert_bp( bp ), intensity, force, deferred );
}