forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
monmove.cpp
2161 lines (1931 loc) · 74.8 KB
/
monmove.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
// Monster movement code; essentially, the AI
#include "monster.h" // IWYU pragma: associated
#include <algorithm>
#include <cfloat>
#include <cmath>
#include <cstdlib>
#include <iterator>
#include <list>
#include <memory>
#include <ostream>
#include <unordered_map>
#include "avatar.h"
#include "behavior.h"
#include "bionics.h"
#include "cata_utility.h"
#include "creature_tracker.h"
#include "debug.h"
#include "effect.h"
#include "field.h"
#include "field_type.h"
#include "game.h"
#include "game_constants.h"
#include "int_id.h"
#include "line.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "mattack_common.h"
#include "messages.h"
#include "monfaction.h"
#include "monster_oracle.h"
#include "mtype.h"
#include "npc.h"
#include "pathfinding.h"
#include "pimpl.h"
#include "player.h"
#include "rng.h"
#include "scent_map.h"
#include "sounds.h"
#include "string_formatter.h"
#include "string_id.h"
#include "tileray.h"
#include "translations.h"
#include "trap.h"
#include "vehicle.h"
#include "vpart_position.h"
static const efftype_id effect_ai_waiting( "ai_waiting" );
static const efftype_id effect_bouldering( "bouldering" );
static const efftype_id effect_countdown( "countdown" );
static const efftype_id effect_docile( "docile" );
static const efftype_id effect_downed( "downed" );
static const efftype_id effect_dragging( "dragging" );
static const efftype_id effect_grabbed( "grabbed" );
static const efftype_id effect_harnessed( "harnessed" );
static const efftype_id effect_no_sight( "no_sight" );
static const efftype_id effect_operating( "operating" );
static const efftype_id effect_pacified( "pacified" );
static const efftype_id effect_pushed( "pushed" );
static const efftype_id effect_stunned( "stunned" );
static const efftype_id effect_led_by_leash( "led_by_leash" );
static const itype_id itype_pressurized_tank( "pressurized_tank" );
static const species_id FUNGUS( "FUNGUS" );
static const species_id INSECT( "INSECT" );
static const species_id SPIDER( "SPIDER" );
static const species_id ZOMBIE( "ZOMBIE" );
static const std::string flag_AUTODOC_COUCH( "AUTODOC_COUCH" );
static const std::string flag_LIQUID( "LIQUID" );
#define MONSTER_FOLLOW_DIST 8
bool monster::wander()
{
return ( goal == pos() );
}
bool monster::is_immune_field( const field_type_id &fid ) const
{
if( fid == fd_fungal_haze ) {
return has_flag( MF_NO_BREATHE ) || type->in_species( FUNGUS );
}
if( fid == fd_fungicidal_gas ) {
return !type->in_species( FUNGUS );
}
if( fid == fd_insecticidal_gas ) {
return !type->in_species( INSECT ) && !type->in_species( SPIDER );
}
const field_type &ft = fid.obj();
if( ft.has_fume ) {
return has_flag( MF_NO_BREATHE );
}
if( ft.has_acid ) {
return has_flag( MF_ACIDPROOF ) || flies();
}
if( ft.has_fire ) {
return has_flag( MF_FIREPROOF );
}
if( ft.has_elec ) {
return has_flag( MF_ELECTRIC );
}
if( ft.immune_mtypes.count( type->id ) > 0 ) {
return true;
}
// No specific immunity was found, so fall upwards
return Creature::is_immune_field( fid );
}
static bool z_is_valid( int z )
{
return z >= -OVERMAP_DEPTH && z <= OVERMAP_HEIGHT;
}
bool monster::will_move_to( const tripoint &p ) const
{
if( g->m.impassable( p ) ) {
if( digging() ) {
if( !g->m.has_flag( "BURROWABLE", p ) ) {
return false;
}
} else if( !( can_climb() && g->m.has_flag( "CLIMBABLE", p ) ) ) {
return false;
}
}
if( ( !can_submerge() && !flies() ) && g->m.has_flag( TFLAG_DEEP_WATER, p ) ) {
return false;
}
if( digs() && !g->m.has_flag( "DIGGABLE", p ) && !g->m.has_flag( "BURROWABLE", p ) ) {
return false;
}
if( has_flag( MF_AQUATIC ) && !g->m.has_flag( "SWIMMABLE", p ) ) {
return false;
}
if( has_flag( MF_SUNDEATH ) && g->is_in_sunlight( p ) ) {
return false;
}
if( get_size() > MS_MEDIUM && g->m.has_flag_ter( TFLAG_SMALL_PASSAGE, p ) ) {
return false; // if a large critter, can't move through tight passages
}
// Various avoiding behaviors.
bool avoid_fire = has_flag( MF_AVOID_FIRE );
bool avoid_fall = has_flag( MF_AVOID_FALL );
bool avoid_simple = has_flag( MF_AVOID_DANGER_1 );
bool avoid_complex = has_flag( MF_AVOID_DANGER_2 );
/*
* Because some avoidance behaviors are supersets of others,
* we can cascade through the implications. Complex implies simple,
* and simple implies fire and fall.
* unfortunately, fall does not necessarily imply fire, nor the converse.
*/
if( avoid_complex ) {
avoid_simple = true;
}
if( avoid_simple ) {
avoid_fire = true;
avoid_fall = true;
}
// technically this will shortcut in evaluation from fire or fall
// before hitting simple or complex but this is more explicit
if( avoid_fire || avoid_fall || avoid_simple || avoid_complex ) {
const ter_id target = g->m.ter( p );
// Don't enter lava if we have any concept of heat being bad
if( avoid_fire && target == t_lava ) {
return false;
}
if( avoid_fall ) {
// Don't throw ourselves off cliffs if we have a concept of falling
if( !g->m.has_floor( p ) && !flies() ) {
return false;
}
// Don't enter open pits ever unless tiny, can fly or climb well
if( !( type->size == MS_TINY || can_climb() ) &&
( target == t_pit || target == t_pit_spiked || target == t_pit_glass ) ) {
return false;
}
}
// Some things are only avoided if we're not attacking
if( attitude( &g->u ) != MATT_ATTACK ) {
// Sharp terrain is ignored while attacking
if( avoid_simple && g->m.has_flag( "SHARP", p ) &&
!( type->size == MS_TINY || flies() ) ) {
return false;
}
}
const field &target_field = g->m.field_at( p );
// Higher awareness is needed for identifying these as threats.
if( avoid_complex ) {
const trap &target_trap = g->m.tr_at( p );
// Don't enter any dangerous fields
if( is_dangerous_fields( target_field ) ) {
return false;
}
// Don't step on any traps (if we can see)
if( has_flag( MF_SEES ) && !target_trap.is_benign() && g->m.has_floor( p ) ) {
return false;
}
}
// Without avoid_complex, only fire and electricity are checked for field avoidance.
if( avoid_fire && target_field.find_field( fd_fire ) ) {
return false;
}
if( avoid_simple && target_field.find_field( fd_electricity ) ) {
return false;
}
}
return true;
}
bool monster::can_reach_to( const tripoint &p ) const
{
map &here = get_map();
if( p.z > pos().z && z_is_valid( pos().z ) ) {
if( here.has_flag( TFLAG_RAMP_UP, tripoint( p.xy(), p.z - 1 ) ) ) {
return true;
}
if( !here.has_flag( TFLAG_GOES_UP, pos() ) && !here.has_flag( TFLAG_NO_FLOOR, p ) ) {
// can't go through the roof
return false;
}
} else if( p.z < pos().z && z_is_valid( pos().z ) ) {
if( !here.has_flag( TFLAG_GOES_DOWN, pos() ) ) {
// can't go through the floor
// you would fall anyway if there was no floor, so no need to check for that here
return false;
}
}
return true;
}
bool monster::can_squeeze_to( const tripoint &p ) const
{
map &m = get_map();
return !m.obstructed_by_vehicle_rotation( pos(), p );
}
bool monster::can_move_to( const tripoint &p ) const
{
return can_reach_to( p ) && will_move_to( p );
}
void monster::set_dest( const tripoint &p )
{
goal = p;
}
void monster::unset_dest()
{
goal = pos();
path.clear();
}
// Move towards p for f more turns--generally if we hear a sound there
// "Stupid" movement; "if (wander_pos.x < posx) posx--;" etc.
void monster::wander_to( const tripoint &p, int f )
{
wander_pos = p;
wandf = f;
}
float monster::rate_target( Creature &c, float best, bool smart ) const
{
const auto d = rl_dist_fast( pos(), c.pos() );
if( d <= 0 ) {
return FLT_MAX;
}
// Check a very common and cheap case first
if( !smart && d >= best ) {
return FLT_MAX;
}
if( !sees( c ) ) {
return FLT_MAX;
}
if( !smart ) {
return int( d );
}
float power = c.power_rating();
monster *mon = dynamic_cast< monster * >( &c );
// Their attitude to us and not ours to them, so that bobcats won't get gunned down
if( mon != nullptr && mon->attitude_to( *this ) == Attitude::A_HOSTILE ) {
power += 2;
}
if( power > 0 ) {
return int( d ) / power;
}
return FLT_MAX;
}
void monster::plan()
{
const auto &factions = g->critter_tracker->factions();
// Bots are more intelligent than most living stuff
bool smart_planning = has_flag( MF_PRIORITIZE_TARGETS );
Creature *target = nullptr;
int max_sight_range = std::max( type->vision_day, type->vision_night );
// 8.6f is rating for tank drone 60 tiles away, moose 16 or boomer 33
float dist = !smart_planning ? max_sight_range : 8.6f;
bool fleeing = false;
bool docile = friendly != 0 && has_effect( effect_docile );
bool waiting = has_effect( effect_ai_waiting );
const bool angers_hostile_weak = type->has_anger_trigger( mon_trigger::HOSTILE_WEAK );
const int angers_hostile_near = type->has_anger_trigger( mon_trigger::HOSTILE_CLOSE ) ? 5 : 0;
const int angers_mating_season = type->has_anger_trigger( mon_trigger::MATING_SEASON ) ? 3 : 0;
const int angers_cub_threatened = type->has_anger_trigger( mon_trigger::PLAYER_NEAR_BABY ) ? 8 : 0;
const int fears_hostile_near = type->has_fear_trigger( mon_trigger::HOSTILE_CLOSE ) ? 5 : 0;
bool group_morale = has_flag( MF_GROUP_MORALE ) && morale < type->morale;
bool swarms = has_flag( MF_SWARMS );
auto mood = attitude();
// If we can see the player, move toward them or flee, simpleminded animals are too dumb to follow the player.
if( friendly == 0 && sees( g->u ) && !waiting ) {
dist = rate_target( g->u, dist, smart_planning );
fleeing = fleeing || is_fleeing( g->u );
target = &g->u;
if( dist <= 5 ) {
anger += angers_hostile_near;
morale -= fears_hostile_near;
if( angers_mating_season > 0 ) {
bool mating_angry = false;
season_type season = season_of_year( calendar::turn );
for( auto &elem : type->baby_flags ) {
if( ( season == SUMMER && elem == "SUMMER" ) ||
( season == WINTER && elem == "WINTER" ) ||
( season == SPRING && elem == "SPRING" ) ||
( season == AUTUMN && elem == "AUTUMN" ) ) {
mating_angry = true;
break;
}
}
if( mating_angry ) {
anger += angers_mating_season;
}
}
}
if( angers_cub_threatened > 0 ) {
for( monster &tmp : g->all_monsters() ) {
if( type->baby_monster == tmp.type->id ) {
// baby nearby; is the player too close?
dist = tmp.rate_target( g->u, dist, smart_planning );
if( dist <= 3 ) {
//proximity to baby; monster gets furious and less likely to flee
anger += angers_cub_threatened;
morale += angers_cub_threatened / 2;
}
}
}
}
} else if( friendly != 0 && !docile && !waiting ) {
for( monster &tmp : g->all_monsters() ) {
if( tmp.friendly == 0 ) {
float rating = rate_target( tmp, dist, smart_planning );
if( rating < dist ) {
target = &tmp;
dist = rating;
}
}
}
}
if( waiting ) {
set_dest( pos() );
return;
}
if( docile ) {
if( friendly != 0 && target != nullptr ) {
set_dest( target->pos() );
}
return;
}
int valid_targets = ( target == nullptr ) ? 1 : 0;
for( npc &who : g->all_npcs() ) {
auto faction_att = faction.obj().attitude( who.get_monster_faction() );
if( faction_att == MFA_NEUTRAL || faction_att == MFA_FRIENDLY ) {
continue;
}
float rating = rate_target( who, dist, smart_planning );
bool fleeing_from = is_fleeing( who );
if( rating == dist && ( fleeing || attitude( &who ) == MATT_ATTACK ) ) {
++valid_targets;
if( one_in( valid_targets ) ) {
target = &who;
}
}
// Switch targets if closer and hostile or scarier than current target
if( ( rating < dist && fleeing ) ||
( faction_att == MFA_HATE ) ||
( rating < dist && attitude( &who ) == MATT_ATTACK ) ||
( !fleeing && fleeing_from ) ) {
target = &who;
dist = rating;
valid_targets = 1;
}
fleeing = fleeing || fleeing_from;
if( rating <= 5 ) {
anger += angers_hostile_near;
morale -= fears_hostile_near;
if( angers_mating_season > 0 ) {
bool mating_angry = false;
season_type season = season_of_year( calendar::turn );
for( auto &elem : type->baby_flags ) {
if( ( season == SUMMER && elem == "SUMMER" ) ||
( season == WINTER && elem == "WINTER" ) ||
( season == SPRING && elem == "SPRING" ) ||
( season == AUTUMN && elem == "AUTUMN" ) ) {
mating_angry = true;
break;
}
}
if( mating_angry ) {
anger += angers_mating_season;
}
}
}
}
fleeing = fleeing || ( mood == MATT_FLEE );
if( friendly == 0 ) {
for( const auto &fac : factions ) {
auto faction_att = faction.obj().attitude( fac.first );
if( faction_att == MFA_NEUTRAL || faction_att == MFA_FRIENDLY ) {
continue;
}
for( const weak_ptr_fast<monster> &weak : fac.second ) {
const shared_ptr_fast<monster> shared = weak.lock();
if( !shared ) {
continue;
}
monster &mon = *shared;
float rating = rate_target( mon, dist, smart_planning );
if( rating == dist ) {
++valid_targets;
if( one_in( valid_targets ) ) {
target = &mon;
}
}
if( rating < dist ) {
target = &mon;
dist = rating;
valid_targets = 1;
}
if( rating <= 5 ) {
anger += angers_hostile_near;
morale -= fears_hostile_near;
}
}
}
}
// Friendly monsters here
// Avoid for hordes of same-faction stuff or it could get expensive
const auto actual_faction = friendly == 0 ? faction : mfaction_str_id( "player" );
const auto &myfaction_iter = factions.find( actual_faction );
if( myfaction_iter == factions.end() ) {
DebugLog( DL::Error, DC::Game ) << disp_name() << " tried to find faction "
<< actual_faction.id().str()
<< " which wasn't loaded in game::monmove";
swarms = false;
group_morale = false;
}
swarms = swarms && target == nullptr; // Only swarm if we have no target
if( group_morale || swarms ) {
for( const weak_ptr_fast<monster> &weak : myfaction_iter->second ) {
const shared_ptr_fast<monster> shared = weak.lock();
if( !shared ) {
continue;
}
monster &mon = *shared;
float rating = rate_target( mon, dist, smart_planning );
if( group_morale && rating <= 10 ) {
morale += 10 - rating;
}
if( swarms ) {
if( rating < 5 ) { // Too crowded here
wander_pos.x = posx() * rng( 1, 3 ) - mon.posx();
wander_pos.y = posy() * rng( 1, 3 ) - mon.posy();
wandf = 2;
target = nullptr;
// Swarm to the furthest ally you can see
} else if( rating < FLT_MAX && rating > dist && wandf <= 0 ) {
target = &mon;
dist = rating;
}
}
}
}
// Operating monster keep you safe while they operate, how nice....
if( type->has_special_attack( "OPERATE" ) ) {
int prev_friendlyness = friendly;
if( has_effect( effect_operating ) ) {
friendly = 100;
for( auto critter : g->m.get_creatures_in_radius( pos(), 6 ) ) {
monster *mon = dynamic_cast<monster *>( critter );
if( mon != nullptr && mon->type->in_species( ZOMBIE ) ) {
anger = 100;
} else {
anger = 0;
}
}
} else {
friendly = prev_friendlyness;
}
}
if( has_effect( effect_dragging ) ) {
if( type->has_special_attack( "OPERATE" ) ) {
bool found_path_to_couch = false;
tripoint tmp( pos() + point( 12, 12 ) );
tripoint couch_loc;
for( const auto &couch_pos : g->m.find_furnitures_or_vparts_with_flag_in_radius( pos(), 10,
flag_AUTODOC_COUCH ) ) {
if( g->m.clear_path( pos(), couch_pos, 10, 0, 100 ) ) {
if( rl_dist( pos(), couch_pos ) < rl_dist( pos(), tmp ) ) {
tmp = couch_pos;
found_path_to_couch = true;
couch_loc = couch_pos;
}
}
}
if( !found_path_to_couch ) {
anger = 0;
remove_effect( effect_dragging );
} else {
set_dest( couch_loc );
}
}
} else if( target != nullptr ) {
tripoint dest = target->pos();
auto att_to_target = attitude_to( *target );
if( att_to_target == Attitude::A_HOSTILE && !fleeing ) {
set_dest( dest );
} else if( fleeing ) {
set_dest( tripoint( posx() * 2 - dest.x, posy() * 2 - dest.y, posz() ) );
}
if( angers_hostile_weak && att_to_target != Attitude::A_FRIENDLY ) {
int hp_per = target->hp_percentage();
if( hp_per <= 70 ) {
anger += 10 - static_cast<int>( hp_per / 10 );
}
}
} else if( friendly > 0 && one_in( 3 ) ) {
// Grow restless with no targets
friendly--;
// if no target, and friendly pet sees the player
} else if( friendly < 0 && sees( g->u ) ) {
// eg dogs
if( !has_flag( MF_PET_WONT_FOLLOW ) ) {
// if too far from the player, go to him
if( rl_dist( pos(), g->u.pos() ) > 2 ) {
set_dest( g->u.pos() );
} else {
unset_dest();
}
// eg cows, horses
} else {
unset_dest();
}
// when the players is close to their pet, it calms them
// it helps them reach an homeostatic state, for morale and anger
const int distance_from_friend = rl_dist( pos(), get_avatar().pos() );
if( distance_from_friend < 12 ) {
if( one_in( distance_from_friend * 3 ) ) {
if( morale != type->morale ) {
morale += ( morale < type->morale ) ? 1 : -1;
}
if( anger != type->agro ) {
anger += ( anger < type->agro ) ? 1 : -1;
}
}
}
}
// being led by a leash override other movements decisions
if( has_effect( effect_led_by_leash ) && friendly != 0 ) {
// if we have an hostile target adjacent to the payer, and we're not fleeing, we can potentially attack it
if( target != nullptr && rl_dist( g->u.pos(), target->pos() ) < 2 &&
target->attitude_to( g->u ) == Attitude::A_HOSTILE && !fleeing ) {
// if we're too far from the player, go back to it
if( rl_dist( pos(), g->u.pos() ) > 5 ) {
set_dest( g->u.pos() );
}
} else if( rl_dist( pos(), g->u.pos() ) > 1 ) {
set_dest( g->u.pos() );
} else {
unset_dest();
}
}
}
/**
* Method to make monster movement speed consistent in the face of staggering behavior and
* differing distance metrics.
* It works by scaling the cost to take a step by
* how much that step reduces the distance to your goal.
* Since it incorporates the current distance metric,
* it also scales for diagonal vs orthogonal movement.
**/
static float get_stagger_adjust( const tripoint &source, const tripoint &destination,
const tripoint &next_step )
{
// TODO: push this down into rl_dist
const float initial_dist =
trigdist ? trig_dist( source, destination ) : rl_dist( source, destination );
const float new_dist =
trigdist ? trig_dist( next_step, destination ) : rl_dist( next_step, destination );
// If we return 0, it wil cancel the action.
return std::max( 0.01f, initial_dist - new_dist );
}
/**
* Returns true if the given square presents a possibility of drowning for the monster: it's deep water, it's liquid,
* the monster can drown, and there is no boardable vehicle part present.
*/
bool monster::is_aquatic_danger( const tripoint &at_pos )
{
return g->m.has_flag_ter( TFLAG_DEEP_WATER, at_pos ) && g->m.has_flag( flag_LIQUID, at_pos ) &&
can_drown() && !g->m.veh_at( at_pos ).part_with_feature( "BOARDABLE", false );
}
bool monster::die_if_drowning( const tripoint &at_pos, const int chance )
{
if( is_aquatic_danger( at_pos ) && one_in( chance ) ) {
die( nullptr );
if( g->u.sees( at_pos ) ) {
add_msg( _( "The %s drowns!" ), name() );
}
return true;
}
return false;
}
// General movement.
// Currently, priority goes:
// 1) Special Attack
// 2) Sight-based tracking
// 3) Scent-based tracking
// 4) Sound-based tracking
void monster::move()
{
// We decrement wandf no matter what. We'll save our wander_to plans until
// after we finish out set_dest plans, UNLESS they time out first.
if( wandf > 0 ) {
wandf--;
}
//Hallucinations have a chance of disappearing each turn
if( is_hallucination() && one_in( 25 ) ) {
die( nullptr );
return;
}
map &here = get_map();
behavior::monster_oracle_t oracle( this );
behavior::tree goals;
goals.add( type->get_goals() );
std::string action = goals.tick( &oracle );
//The monster can consume objects it stands on. Check if there are any.
//If there are. Consume them.
// TODO: Stick this in a map and dispatch to it via the action string.
if( action == "consume_items" ) {
if( g->u.sees( *this ) ) {
add_msg( _( "The %s flows around the objects on the floor and they are quickly dissolved!" ),
name() );
}
static const auto volume_per_hp = 250_ml;
for( auto &elem : g->m.i_at( pos() ) ) {
hp += elem.volume() / volume_per_hp; // Yeah this means it can get more HP than normal.
if( has_flag( MF_ABSORBS_SPLITS ) ) {
while( hp / 2 > type->hp ) {
monster *const spawn = g->place_critter_around( type->id, pos(), 1 );
if( !spawn ) {
break;
}
hp -= type->hp;
//this is a new copy of the monster. Ideally we should copy the stats/effects that affect the parent
spawn->make_ally( *this );
if( g->u.sees( *this ) ) {
add_msg( _( "The %s splits in two!" ), name() );
}
}
}
}
g->m.i_clear( pos() );
}
// record position before moving to put the player there if we're dragging
tripoint drag_to = g->m.getabs( pos() );
const bool pacified = has_effect( effect_pacified );
// First, use the special attack, if we can!
// The attack may change `monster::special_attacks` (e.g. by transforming
// this into another monster type). Therefore we can not iterate over it
// directly and instead iterate over the map from the monster type
// (properties of monster types should never change).
for( const auto &sp_type : type->special_attacks ) {
const std::string &special_name = sp_type.first;
const auto local_iter = special_attacks.find( special_name );
if( local_iter == special_attacks.end() ) {
continue;
}
mon_special_attack &local_attack_data = local_iter->second;
if( !local_attack_data.enabled ) {
continue;
}
// Cooldowns are decremented in monster::process_turn
if( local_attack_data.cooldown == 0 && !pacified && !is_hallucination() ) {
if( !sp_type.second->call( *this ) ) {
continue;
}
// `special_attacks` might have changed at this point. Sadly `reset_special`
// doesn't check the attack name, so we need to do it here.
if( special_attacks.count( special_name ) == 0 ) {
continue;
}
reset_special( special_name );
}
}
// Check if they're dragging a foe and find their hapless victim
player *dragged_foe = find_dragged_foe();
// Give nursebots a chance to do surgery.
nursebot_operate( dragged_foe );
// The monster can sometimes hang in air due to last fall being blocked
if( !flies() && g->m.has_flag( TFLAG_NO_FLOOR, pos() ) ) {
g->m.creature_on_trap( *this, false );
if( is_dead() ) {
return;
}
}
// if the monster is in a deep water tile, it has a chance to drown
if( die_if_drowning( pos(), 10 ) ) {
return;
}
if( moves < 0 ) {
return;
}
// TODO: Move this to attack_at/move_to/etc. functions
bool attacking = false;
if( !move_effects( attacking ) ) {
moves = 0;
return;
}
if( has_flag( MF_IMMOBILE ) || has_flag( MF_RIDEABLE_MECH ) ) {
moves = 0;
return;
}
if( has_effect( effect_stunned ) ) {
stumble();
moves = 0;
return;
}
if( friendly > 0 ) {
--friendly;
}
if( has_effect( effect_ai_waiting ) ) {
moves = 0;
return;
}
// don't move if a passenger in a moving vehicle
auto vp = g->m.veh_at( pos() );
bool harness_part = static_cast<bool>( g->m.veh_at( pos() ).part_with_feature( "ANIMAL_CTRL",
true ) );
if( vp && vp->vehicle().is_moving() && vp->vehicle().get_pet( vp->part_index() ) ) {
moves = 0;
return;
// Don't move if harnessed, even if vehicle is stationary
} else if( vp && has_effect( effect_harnessed ) ) {
moves = 0;
return;
// If harnessed monster finds itself moved from the harness point, the harness probably broke!
} else if( !harness_part && has_effect( effect_harnessed ) ) {
remove_effect( effect_harnessed );
}
// Set attitude to attitude to our current target
monster_attitude current_attitude = attitude( nullptr );
if( !wander() ) {
if( goal == g->u.pos() ) {
current_attitude = attitude( &g->u );
} else {
for( const npc &guy : g->all_npcs() ) {
if( goal == guy.pos() ) {
current_attitude = attitude( &guy );
}
}
}
}
if( current_attitude == MATT_IGNORE ||
( current_attitude == MATT_FOLLOW && rl_dist( pos(), goal ) <= MONSTER_FOLLOW_DIST ) ) {
moves -= 100;
stumble();
return;
}
bool moved = false;
tripoint destination;
bool try_to_move = false;
for( const tripoint &dest : g->m.points_in_radius( pos(), 1 ) ) {
if( dest != pos() ) {
if( can_move_to( dest ) && can_squeeze_to( dest ) &&
g->critter_at( dest, true ) == nullptr ) {
try_to_move = true;
break;
}
}
}
// If true, don't try to greedily avoid locally bad paths
bool pathed = false;
if( try_to_move ) {
if( !wander() ) {
while( !path.empty() && path.front() == pos() ) {
path.erase( path.begin() );
}
const auto &pf_settings = get_pathfinding_settings();
if( pf_settings.max_dist >= rl_dist( pos(), goal ) &&
( path.empty() || rl_dist( pos(), path.front() ) >= 2 || path.back() != goal ) ) {
// We need a new path
path = g->m.route( pos(), goal, pf_settings, get_path_avoid() );
}
// Try to respect old paths, even if we can't pathfind at the moment
if( !path.empty() && path.back() == goal ) {
destination = path.front();
moved = true;
pathed = true;
} else {
// Straight line forward, probably because we can't pathfind (well enough)
destination = goal;
moved = true;
}
}
}
if( !moved && has_flag( MF_SMELLS ) ) {
// No sight... or our plans are invalid (e.g. moving through a transparent, but
// solid, square of terrain). Fall back to smell if we have it.
unset_dest();
tripoint tmp = scent_move();
if( tmp.x != -1 ) {
destination = tmp;
moved = true;
}
}
if( wandf > 0 && !moved && friendly == 0 ) { // No LOS, no scent, so as a fall-back follow sound
unset_dest();
if( wander_pos != pos() ) {
destination = wander_pos;
moved = true;
}
}
if( !g->m.has_zlevels() ) {
// Otherwise weird things happen
destination.z = posz();
}
point new_d( destination.xy() - pos().xy() );
// toggle facing direction for sdl flip
if( !tile_iso ) {
if( new_d.x < 0 ) {
facing = FD_LEFT;
} else if( new_d.x > 0 ) {
facing = FD_RIGHT;
}
} else {
if( new_d.y <= 0 && new_d.x <= 0 ) {
facing = FD_LEFT;
}
if( new_d.x >= 0 && new_d.y >= 0 ) {
facing = FD_RIGHT;
}
}
tripoint next_step;
const bool can_open_doors = has_flag( MF_CAN_OPEN_DOORS );
const bool staggers = has_flag( MF_STUMBLES );
if( moved ) {
// Implement both avoiding obstacles and staggering.
moved = false;
float switch_chance = 0.0;
const bool can_bash = bash_skill() > 0;
// This is a float and using trig_dist() because that Does the Right Thing(tm)
// in both circular and roguelike distance modes.
const float distance_to_target = trig_dist( pos(), destination );
for( tripoint &candidate : squares_closer_to( pos(), destination ) ) {
bool via_ramp = false;
if( here.has_flag( TFLAG_RAMP_UP, candidate ) ) {
via_ramp = true;
candidate.z += 1;
} else if( here.has_flag( TFLAG_RAMP_DOWN, candidate ) ) {
via_ramp = true;
candidate.z -= 1;
}
tripoint candidate_abs = g->m.getabs( candidate );
bool can_z_move = true;
if( candidate.z != posz() ) {
bool can_z_attack = true;
if( !here.valid_move( pos(), candidate, false, true, via_ramp ) ) {
// Can't phase through floor
can_z_move = false;
can_z_attack = false;
}
// If we're trying to go up but can't fly, check if we can climb. If we can't, then don't
// This prevents non-climb/fly enemies running up walls
if( can_z_move && candidate.z > posz() && !( via_ramp || flies() ) &&
( !can_climb() || !here.has_floor_or_support( candidate ) ) ) {
// Can't "jump" up a whole z-level
can_z_move = false;
}
// We can still do the z-level stair teleport bullshit that isn't removed yet
// TODO: Remove z-level stair bullshit teleport after aligning all stairs
if( !can_z_move &&
posx() / ( SEEX * 2 ) == candidate.x / ( SEEX * 2 ) &&
posy() / ( SEEY * 2 ) == candidate.y / ( SEEY * 2 ) ) {
const tripoint &upper = candidate.z > posz() ? candidate : pos();
const tripoint &lower = candidate.z > posz() ? pos() : candidate;
if( g->m.has_flag( TFLAG_GOES_DOWN, upper ) && g->m.has_flag( TFLAG_GOES_UP, lower ) ) {
can_z_move = true;
}
}
if( !can_z_attack ) {
continue;
}
}
// A flag to allow non-stumbling critters to stumble when the most direct choice is bad.
bool bad_choice = false;
const Creature *target = g->critter_at( candidate, is_hallucination() );
if( target != nullptr ) {
const Creature::Attitude att = attitude_to( *target );
if( att == A_HOSTILE ) {
// When attacking an adjacent enemy, we're direct.
moved = true;
next_step = candidate_abs;
break;
} else if( att == A_FRIENDLY && ( target->is_player() || target->is_npc() ) ) {
continue; // Friendly firing the player or an NPC is illegal for gameplay reasons
} else if( !has_flag( MF_ATTACKMON ) && !has_flag( MF_PUSH_MON ) ) {
// Bail out if there's a non-hostile monster in the way and we're not pushy.
continue;
}
// Friendly fire and pushing are always bad choices - they take a lot of time
bad_choice = true;