forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_spell_effect.cpp
1752 lines (1575 loc) · 63.4 KB
/
magic_spell_effect.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 <algorithm>
#include <array>
#include <climits>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <iosfwd>
#include <iterator>
#include <map>
#include <memory>
#include <new>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include "avatar.h"
#include "bodypart.h"
#include "calendar.h"
#include "character.h"
#include "colony.h"
#include "color.h"
#include "coordinates.h"
#include "creature.h"
#include "creature_tracker.h"
#include "damage.h"
#include "debug.h"
#include "effect_on_condition.h"
#include "enums.h"
#include "explosion.h"
#include "field.h"
#include "field_type.h"
#include "fungal_effects.h"
#include "game.h"
#include "item.h"
#include "kill_tracker.h"
#include "line.h"
#include "magic.h"
#include "magic_spell_effect_helpers.h"
#include "magic_teleporter_list.h"
#include "magic_ter_furn_transform.h"
#include "map.h"
#include "map_iterator.h"
#include "messages.h"
#include "mongroup.h"
#include "monster.h"
#include "monstergenerator.h"
#include "morale.h"
#include "mtype.h"
#include "npc.h"
#include "optional.h"
#include "overmapbuffer.h"
#include "pimpl.h"
#include "point.h"
#include "projectile.h"
#include "ret_val.h"
#include "rng.h"
#include "string_formatter.h"
#include "teleport.h"
#include "timed_event.h"
#include "translations.h"
#include "type_id.h"
#include "units.h"
#include "vehicle.h"
#include "vpart_position.h"
static const efftype_id effect_teleglow( "teleglow" );
static const flag_id json_flag_FIT( "FIT" );
static const json_character_flag json_flag_PRED1( "PRED1" );
static const json_character_flag json_flag_PRED2( "PRED2" );
static const json_character_flag json_flag_PRED3( "PRED3" );
static const json_character_flag json_flag_PRED4( "PRED4" );
static const mtype_id mon_blob( "mon_blob" );
static const mtype_id mon_blob_brain( "mon_blob_brain" );
static const mtype_id mon_blob_large( "mon_blob_large" );
static const mtype_id mon_blob_small( "mon_blob_small" );
static const mtype_id mon_generator( "mon_generator" );
static const species_id species_HALLUCINATION( "HALLUCINATION" );
static const species_id species_SLIME( "SLIME" );
static const trait_id trait_KILLER( "KILLER" );
static const trait_id trait_PACIFIST( "PACIFIST" );
static const trait_id trait_PSYCHOPATH( "PSYCHOPATH" );
namespace spell_detail
{
struct line_iterable {
const std::vector<point> &delta_line;
point cur_origin;
point delta;
size_t index;
line_iterable( const point &origin, const point &delta, const std::vector<point> &dline )
: delta_line( dline ), cur_origin( origin ), delta( delta ), index( 0 ) {}
point get() const {
return cur_origin + delta_line[index];
}
// Move forward along point set, wrap around and move origin forward if necessary
void next() {
index = ( index + 1 ) % delta_line.size();
cur_origin = cur_origin + delta * ( index == 0 );
}
// Move back along point set, wrap around and move origin backward if necessary
void prev() {
cur_origin = cur_origin - delta * ( index == 0 );
index = ( index + delta_line.size() - 1 ) % delta_line.size();
}
void reset( const point &origin ) {
cur_origin = origin;
index = 0;
}
};
// Orientation of point C relative to line AB
static int side_of( const point &a, const point &b, const point &c )
{
int cross = ( b.x - a.x ) * ( c.y - a.y ) - ( b.y - a.y ) * ( c.x - a.x );
return ( cross > 0 ) - ( cross < 0 );
}
// Tests if point c is between or on lines (a0, a0 + d) and (a1, a1 + d)
static bool between_or_on( const point &a0, const point &a1, const point &d, const point &c )
{
return side_of( a0, a0 + d, c ) != 1 && side_of( a1, a1 + d, c ) != -1;
}
// Builds line until obstructed or outside of region bound by near and far lines. Stores result in set
static void build_line( spell_detail::line_iterable line, const tripoint &source,
const point &delta, const point &delta_perp, bool ( *test )( const tripoint & ),
std::set<tripoint> &result )
{
while( between_or_on( point_zero, delta, delta_perp, line.get() ) ) {
if( !test( source + line.get() ) ) {
break;
}
result.emplace( source + line.get() );
line.next();
}
}
} // namespace spell_detail
void spell_effect::short_range_teleport( const spell &sp, Creature &caster, const tripoint &target )
{
const bool safe = !sp.has_flag( spell_flag::UNSAFE_TELEPORT );
const bool target_teleport = sp.has_flag( spell_flag::TARGET_TELEPORT );
if( target_teleport ) {
if( sp.aoe() == 0 ) {
teleport::teleport_to_point( caster, target, safe, false );
return;
}
std::set<tripoint> potential_targets = calculate_spell_effect_area( sp, target, caster );
tripoint where = random_entry( potential_targets );
teleport::teleport_to_point( caster, where, safe, false );
return;
}
const int min_distance = sp.range();
const int max_distance = sp.range() + sp.aoe();
if( min_distance > max_distance || min_distance < 0 || max_distance < 0 ) {
debugmsg( "ERROR: Teleport argument(s) invalid" );
return;
}
teleport::teleport( caster, min_distance, max_distance, safe, false );
}
static void swap_pos( Creature &caster, const tripoint &target )
{
Creature *const critter = get_creature_tracker().creature_at<Creature>( target );
critter->setpos( caster.pos() );
caster.setpos( target );
//update map in case a monster swapped positions with the player
g->update_map( get_player_character() );
}
void spell_effect::pain_split( const spell &sp, Creature &caster, const tripoint & )
{
Character *you = caster.as_character();
if( you == nullptr ) {
return;
}
sp.make_sound( caster.pos() );
add_msg( m_info, _( "Your injuries even out." ) );
int num_limbs = 0; // number of limbs effected (broken don't count)
int total_hp = 0; // total hp among limbs
for( const std::pair<const bodypart_str_id, bodypart> &elem : you->get_body() ) {
if( elem.first == bodypart_str_id::NULL_ID() ) {
continue;
}
num_limbs++;
total_hp += elem.second.get_hp_cur();
}
const int hp_each = total_hp / num_limbs;
you->set_all_parts_hp_cur( hp_each );
}
static bool in_spell_aoe( const tripoint &start, const tripoint &end, const int &radius,
const bool ignore_walls )
{
if( rl_dist( start, end ) > radius ) {
return false;
}
if( ignore_walls ) {
return true;
}
map &here = get_map();
const std::vector<tripoint> trajectory = line_to( start, end );
for( const tripoint &pt : trajectory ) {
if( here.impassable( pt ) ) {
return false;
}
}
return true;
}
std::set<tripoint> spell_effect::spell_effect_blast( const override_parameters ¶ms,
const tripoint &,
const tripoint &target )
{
std::set<tripoint> targets;
// TODO: Make this breadth-first
for( const tripoint &potential_target : get_map().points_in_radius( target, params.aoe_radius ) ) {
if( in_spell_aoe( target, potential_target, params.aoe_radius, params.ignore_walls ) ) {
targets.emplace( potential_target );
}
}
return targets;
}
static std::set<tripoint> spell_effect_cone_range_override( const spell_effect::override_parameters
¶ms,
const tripoint &source, const tripoint &target )
{
std::set<tripoint> targets;
const units::angle initial_angle = coord_to_angle( source, target );
const units::angle half_width = units::from_degrees( params.aoe_radius / 2.0 );
const units::angle start_angle = initial_angle - half_width;
const units::angle end_angle = initial_angle + half_width;
std::set<tripoint> end_points;
for( units::angle angle = start_angle; angle <= end_angle; angle += 1_degrees ) {
for( int range = 1; range <= params.range; range++ ) {
tripoint potential;
calc_ray_end( angle, range, source, potential );
if( params.ignore_walls ) {
targets.emplace( potential );
} else {
end_points.emplace( potential );
}
}
}
if( !params.ignore_walls ) {
map &here = get_map();
for( const tripoint &ep : end_points ) {
std::vector<tripoint> trajectory = line_to( source, ep );
for( const tripoint &tp : trajectory ) {
if( here.passable( tp ) ) {
targets.emplace( tp );
} else {
break;
}
}
}
}
// we don't want to hit ourselves in the blast!
targets.erase( source );
return targets;
}
std::set<tripoint> spell_effect::spell_effect_cone( const override_parameters ¶ms,
const tripoint &source,
const tripoint &target )
{
// cones go all the way to end (if they don't hit an obstacle)
return spell_effect_cone_range_override( params, source, target );
}
static bool test_always_true( const tripoint & )
{
return true;
}
static bool test_passable( const tripoint &p )
{
return get_map().passable( p );
}
std::set<tripoint> spell_effect::spell_effect_line( const override_parameters ¶ms,
const tripoint &source,
const tripoint &target )
{
const point delta = ( target - source ).xy();
const int dist = square_dist( point_zero, delta );
// Early out to prevent unnecessary calculations
if( dist == 0 ) {
return std::set<tripoint>();
}
// Clockwise Perpendicular of Delta vector
const point delta_perp( -delta.y, delta.x );
const point abs_delta = delta.abs();
// Primary axis of delta vector
const point axis_delta = abs_delta.x > abs_delta.y ? point( delta.x, 0 ) : point( 0, delta.y );
// Clockwise Perpendicular of axis vector
const point cw_perp_axis( -axis_delta.y, axis_delta.x );
const point unit_cw_perp_axis( sgn( cw_perp_axis.x ), sgn( cw_perp_axis.y ) );
// bias leg length toward cw side if uneven
int ccw_len = params.aoe_radius / 2;
int cw_len = params.aoe_radius - ccw_len;
if( !trigdist ) {
ccw_len = ( ccw_len * ( abs_delta.x + abs_delta.y ) ) / dist;
cw_len = ( cw_len * ( abs_delta.x + abs_delta.y ) ) / dist;
}
// is delta aligned with, cw, or ccw of primary axis
int delta_side = spell_detail::side_of( point_zero, axis_delta, delta );
bool ( *test )( const tripoint & ) = params.ignore_walls ? test_always_true : test_passable;
// Canonical path from source to target, offset to local space
std::vector<point> path_to_target = line_to( point_zero, delta );
// Remove endpoint,
path_to_target.pop_back();
// and insert startpoint. Path is now prepared for wrapped iteration
path_to_target.insert( path_to_target.begin(), point_zero );
spell_detail::line_iterable base_line( point_zero, delta, path_to_target );
std::set<tripoint> result;
// Add midline points (source -> target )
spell_detail::build_line( base_line, source, delta, delta_perp, test, result );
// Add cw and ccw legs
if( delta_side == 0 ) { // delta is already axis aligned, only need straight lines
// cw leg
for( const point &p : line_to( point_zero, unit_cw_perp_axis * cw_len ) ) {
base_line.reset( p );
if( !test( source + p ) ) {
break;
}
spell_detail::build_line( base_line, source, delta, delta_perp, test, result );
}
// ccw leg
for( const point &p : line_to( point_zero, unit_cw_perp_axis * -ccw_len ) ) {
base_line.reset( p );
if( !test( source + p ) ) {
break;
}
spell_detail::build_line( base_line, source, delta, delta_perp, test, result );
}
} else if( delta_side == 1 ) { // delta is cw of primary axis
// ccw leg is behind perp axis
for( const point &p : line_to( point_zero, unit_cw_perp_axis * -ccw_len ) ) {
base_line.reset( p );
// forward until in
while( spell_detail::side_of( point_zero, delta_perp, base_line.get() ) == 1 ) {
base_line.next();
}
if( !test( source + p ) ) {
break;
}
spell_detail::build_line( base_line, source, delta, delta_perp, test, result );
}
// cw leg is before perp axis
for( const point &p : line_to( point_zero, unit_cw_perp_axis * cw_len ) ) {
base_line.reset( p );
// move back
while( spell_detail::side_of( point_zero, delta_perp, base_line.get() ) != 1 ) {
base_line.prev();
}
base_line.next();
if( !test( source + p ) ) {
break;
}
spell_detail::build_line( base_line, source, delta, delta_perp, test, result );
}
} else if( delta_side == -1 ) { // delta is ccw of primary axis
// ccw leg is before perp axis
for( const point &p : line_to( point_zero, unit_cw_perp_axis * -ccw_len ) ) {
base_line.reset( p );
// move back
while( spell_detail::side_of( point_zero, delta_perp, base_line.get() ) != 1 ) {
base_line.prev();
}
base_line.next();
if( !test( source + p ) ) {
break;
}
spell_detail::build_line( base_line, source, delta, delta_perp, test, result );
}
// cw leg is behind perp axis
for( const point &p : line_to( point_zero, unit_cw_perp_axis * cw_len ) ) {
base_line.reset( p );
// forward until in
while( spell_detail::side_of( point_zero, delta_perp, base_line.get() ) == 1 ) {
base_line.next();
}
if( !test( source + p ) ) {
break;
}
spell_detail::build_line( base_line, source, delta, delta_perp, test, result );
}
}
result.erase( source );
return result;
}
// spells do not reduce in damage the further away from the epicenter the targets are
// rather they do their full damage in the entire area of effect
std::set<tripoint> calculate_spell_effect_area( const spell &sp, const tripoint &target,
const Creature &caster )
{
// the actual target that the spell will hit.
tripoint epicenter( target );
// stop short if we hit a wall, if the spell has a projectile
if( sp.shape() == spell_shape::blast && !sp.has_flag( spell_flag::NO_PROJECTILE ) ) {
std::vector<tripoint> trajectory = line_to( caster.pos(), target );
for( std::vector<tripoint>::iterator iter = trajectory.begin(); iter != trajectory.end(); iter++ ) {
if( get_map().impassable( *iter ) ) {
if( iter != trajectory.begin() ) {
epicenter = *( iter - 1 );
} else {
epicenter = *iter;
}
break;
}
}
}
std::set<tripoint> targets = { epicenter }; // initialize with epicenter
if( sp.aoe() <= 1 && sp.shape() != spell_shape::line ) {
return targets;
}
targets = sp.effect_area( caster.pos(), target );
for( std::set<tripoint>::iterator it = targets.begin(); it != targets.end(); ) {
if( !sp.is_valid_target( caster, *it ) ) {
it = targets.erase( it );
} else {
++it;
}
}
return targets;
}
static std::set<tripoint> spell_effect_area( const spell &sp, const tripoint &target,
const Creature &caster )
{
// calculate spell's effect area
std::set<tripoint> targets = calculate_spell_effect_area( sp, target, caster );
// Draw the explosion
std::map<tripoint, nc_color> explosion_colors;
for( const tripoint &pt : targets ) {
explosion_colors[pt] = sp.damage_type_color();
}
std::string exp_name = "explosion_" + sp.id().str();
explosion_handler::draw_custom_explosion( get_player_character().pos(), explosion_colors,
exp_name );
return targets;
}
static void add_effect_to_target( const tripoint &target, const spell &sp )
{
const int dur_moves = sp.duration();
const time_duration dur_td = time_duration::from_moves( dur_moves );
creature_tracker &creatures = get_creature_tracker();
Creature *const critter = creatures.creature_at<Creature>( target );
Character *const guy = creatures.creature_at<Character>( target );
efftype_id spell_effect( sp.effect_data() );
bool bodypart_effected = false;
if( guy ) {
for( const bodypart_id &bp : guy->get_all_body_parts() ) {
if( sp.bp_is_affected( bp.id() ) ) {
guy->add_effect( spell_effect, dur_td, bp, sp.has_flag( spell_flag::PERMANENT ) );
bodypart_effected = true;
}
}
}
if( !bodypart_effected ) {
critter->add_effect( spell_effect, dur_td );
}
}
static void damage_targets( const spell &sp, Creature &caster,
const std::set<tripoint> &targets )
{
map &here = get_map();
creature_tracker &creatures = get_creature_tracker();
for( const tripoint &target : targets ) {
if( !sp.is_valid_target( caster, target ) ) {
continue;
}
sp.make_sound( target );
sp.create_field( target );
if( sp.has_flag( spell_flag::IGNITE_FLAMMABLE ) && here.is_flammable( target ) ) {
here.add_field( target, fd_fire, 1, 10_minutes );
}
Creature *const cr = creatures.creature_at<Creature>( target );
if( !cr ) {
continue;
}
dealt_projectile_attack atk = sp.get_projectile_attack( target, *cr );
if( !sp.effect_data().empty() ) {
add_effect_to_target( target, sp );
}
if( sp.damage() > 0 ) {
cr->deal_projectile_attack( &caster, atk, true );
} else if( sp.damage() < 0 ) {
sp.heal( target );
add_msg_if_player_sees( cr->pos(), m_good, _( "%s wounds are closing up!" ),
cr->disp_name( true ) );
}
// TODO: randomize hit location
cr->add_damage_over_time( sp.damage_over_time( { body_part_torso } ) );
}
}
void spell_effect::attack( const spell &sp, Creature &caster,
const tripoint &epicenter )
{
damage_targets( sp, caster, spell_effect_area( sp, epicenter, caster ) );
if( sp.has_flag( spell_flag::SWAP_POS ) ) {
swap_pos( caster, epicenter );
}
}
static void magical_polymorph( monster &victim, Creature &caster, const spell &sp )
{
mtype_id new_id = mtype_id( sp.effect_data() );
if( sp.has_flag( spell_flag::POLYMORPH_GROUP ) ) {
const mongroup_id group_id( sp.effect_data() );
new_id = MonsterGroupManager::GetRandomMonsterFromGroup( group_id );
}
// if effect_str is empty, we become a random monster of close difficulty
if( new_id.is_empty() ) {
int victim_diff = victim.type->difficulty;
const std::vector<mtype> &mtypes = MonsterGenerator::generator().get_all_mtypes();
for( int difficulty_variance = 1; difficulty_variance < 2048; difficulty_variance *= 2 ) {
unsigned int random_entry = rng( 0, mtypes.size() );
unsigned int iter = random_entry + 1;
while( iter != random_entry && new_id.is_empty() ) {
if( iter >= mtypes.size() ) {
iter = 0;
}
if( ( mtypes[iter].id != victim.type->id ) && ( std::abs( mtypes[iter].difficulty - victim_diff )
<= difficulty_variance ) ) {
if( !mtypes[iter].in_species( species_HALLUCINATION ) &&
mtypes[iter].id != mon_generator ) {
new_id = mtypes[iter].id;
break;
}
}
iter++;
}
}
}
if( !new_id.is_valid() ) {
debugmsg( "magical_polymorph called with an invalid monster id" );
return;
}
add_msg_if_player_sees( victim, _( "The %s transforms into a %s." ),
victim.type->nname(), new_id->nname() );
victim.poly( new_id );
if( sp.has_flag( spell_flag::FRIENDLY_POLY ) ) {
if( caster.as_character() ) {
victim.friendly = -1;
} else {
victim.make_ally( *caster.as_monster() );
}
}
}
void spell_effect::targeted_polymorph( const spell &sp, Creature &caster, const tripoint &target )
{
//we only target monsters for now.
if( monster *const victim = get_creature_tracker().creature_at<monster>( target ) ) {
if( victim->get_hp() < sp.damage() ) {
magical_polymorph( *victim, caster, sp );
return;
}
}
//victim had high hp, or isn't a monster.
caster.add_msg_if_player( m_bad, _( "Your target resists transformation." ) );
}
area_expander::area_expander() : frontier( area_node_comparator( area ) )
{
}
// Check whether we have already visited this node.
int area_expander::contains( const tripoint &pt ) const
{
return area_search.count( pt ) > 0;
}
// Adds node to a search tree. Returns true if new node is allocated.
bool area_expander::enqueue( const tripoint &from, const tripoint &to, float cost )
{
if( contains( to ) ) {
// We will modify existing node if its cost is lower.
int index = area_search[to];
node &node = area[index];
if( cost < node.cost ) {
node.from = from;
node.cost = cost;
}
return false;
}
int index = area.size();
area.push_back( {to, from, cost} );
frontier.push( index );
area_search[to] = index;
return true;
}
// Run wave propagation
int area_expander::run( const tripoint ¢er )
{
enqueue( center, center, 0.0 );
static constexpr std::array<int, 8> x_offset = {{ -1, 1, 0, 0, 1, -1, -1, 1 }};
static constexpr std::array<int, 8> y_offset = {{ 0, 0, -1, 1, -1, 1, -1, 1 }};
// Number of nodes expanded.
int expanded = 0;
map &here = get_map();
while( !frontier.empty() ) {
int best_index = frontier.top();
frontier.pop();
for( size_t i = 0; i < 8; i++ ) {
node &best = area[best_index];
const tripoint &pt = best.position + point( x_offset[ i ], y_offset[ i ] );
if( here.impassable( pt ) ) {
continue;
}
float center_range = static_cast<float>( rl_dist( center, pt ) );
if( max_range > 0 && center_range > max_range ) {
continue;
}
if( max_expand > 0 && expanded > max_expand && contains( pt ) ) {
continue;
}
float delta_range = trig_dist( best.position, pt );
if( enqueue( best.position, pt, best.cost + delta_range ) ) {
expanded++;
}
}
}
return expanded;
}
// Sort nodes by its cost.
void area_expander::sort_ascending()
{
// Since internal caches like 'area_search' and 'frontier' use indexes inside 'area',
// these caches will be invalidated.
std::sort( area.begin(), area.end(),
[]( const node & a, const node & b ) -> bool {
return a.cost < b.cost;
} );
}
void area_expander::sort_descending()
{
// Since internal caches like 'area_search' and 'frontier' use indexes inside 'area',
// these caches will be invalidated.
std::sort( area.begin(), area.end(),
[]( const node & a, const node & b ) -> bool {
return a.cost > b.cost;
} );
}
static void move_items( map &here, const tripoint &from, const tripoint &to )
{
for( const item &it : here.i_at( from ) ) {
here.add_item( to, it );
}
here.i_clear( from );
}
static void move_field( map &here, const tripoint &from, const tripoint &to )
{
field &src_field = here.field_at( from );
std::map<field_type_id, int> moving_fields;
for( const std::pair<const field_type_id, field_entry> &fd : src_field ) {
if( fd.first.is_valid() && !fd.first.id().is_null() ) {
const int intensity = fd.second.get_field_intensity();
moving_fields.emplace( fd.first, intensity );
}
}
for( const std::pair<const field_type_id, int> &fd : moving_fields ) {
here.remove_field( from, fd.first );
here.set_field_intensity( to, fd.first, fd.second );
}
}
// Moving all objects from one point to another by the power of magic.
static void spell_move( const spell &sp, const Creature &caster,
const tripoint &from, const tripoint &to )
{
if( from == to ) {
return;
}
// Moving creatures
const bool can_target_self = sp.is_valid_target( spell_target::self );
const bool can_target_ally = sp.is_valid_target( spell_target::ally );
const bool can_target_hostile = sp.is_valid_target( spell_target::hostile );
const bool can_target_creature = can_target_self || can_target_ally || can_target_hostile;
if( can_target_creature ) {
if( Creature *victim = get_creature_tracker().creature_at<Creature>( from ) ) {
Creature::Attitude cr_att = victim->attitude_to( get_player_character() );
bool valid = cr_att != Creature::Attitude::FRIENDLY && can_target_hostile;
if( victim == &caster ) {
valid = can_target_self;
} else {
valid |= cr_att == Creature::Attitude::FRIENDLY && can_target_ally;
valid |= victim == &caster && can_target_self;
}
if( valid ) {
victim->knock_back_to( to );
}
}
}
// Moving items
if( sp.is_valid_target( spell_target::item ) ) {
move_items( get_map(), from, to );
}
// Moving fields.
if( sp.is_valid_target( spell_target::field ) ) {
move_field( get_map(), from, to );
}
}
static std::pair<field, tripoint> spell_remove_field( const spell &sp,
const field_type_id &target_field_type_id,
const tripoint ¢er )
{
::map &here = get_map();
area_expander expander;
expander.max_range = sp.aoe();
expander.run( center );
expander.sort_ascending();
field field_removed = field();
tripoint field_position = tripoint();
bool did_field_removal = false;
for( const auto &node : expander.area ) {
if( node.from == node.position ) {
continue;
}
field &target_field = here.field_at( node.position );
for( const std::pair<const field_type_id, field_entry> &fd : target_field ) {
if( fd.first.is_valid() && !fd.first.id().is_null() &&
fd.second.get_field_type() == target_field_type_id ) {
field_removed = target_field;
field_position = node.position;
here.remove_field( node.position, target_field_type_id );
did_field_removal = true;
}
}
if( did_field_removal ) {
// only remove one field in case of multiple
break;
}
}
return std::pair<field, tripoint> {field_removed, field_position};
}
static void handle_remove_fd_fatigue_field( const std::pair<field, tripoint> &fd_fatigue_field,
Creature &caster )
{
for( const std::pair<const field_type_id, field_entry> &fd : std::get<0>( fd_fatigue_field ) ) {
const int &intensity = fd.second.get_field_intensity();
const translation &intensity_name = fd.second.get_intensity_level().name;
const tripoint &field_position = std::get<1>( fd_fatigue_field );
const bool sees_field = caster.sees( field_position );
switch( intensity ) {
case 1:
if( sees_field ) {
caster.add_msg_if_player( m_neutral, _( "The %s fades. You feel strange." ), intensity_name );
} else {
caster.add_msg_if_player( m_neutral, _( "You suddenly feel strange." ) );
}
caster.add_effect( effect_teleglow, 1_hours );
break;
case 2:
if( sees_field ) {
caster.add_msg_if_player( m_neutral, _( "The %s dissipates. You feel strange." ), intensity_name );
} else {
caster.add_msg_if_player( m_neutral, _( "You suddenly feel strange." ) );
}
caster.add_effect( effect_teleglow, 5_hours );
break;
case 3:
std::string message_prefix = "A nearby";
if( caster.sees( field_position ) ) {
message_prefix = "The";
}
caster.add_msg_if_player( m_bad,
_( "%s %s pulls you in as it closes and ejects you violently!" ),
message_prefix, intensity_name );
caster.as_character()->hurtall( 10, nullptr );
caster.add_effect( effect_teleglow, 630_minutes );
teleport::teleport( caster );
break;
}
break;
}
}
void spell_effect::remove_field( const spell &sp, Creature &caster, const tripoint ¢er )
{
const field_type_id &target_field_type_id = field_type_id( sp.effect_data() );
std::pair<field, tripoint> field_removed = spell_remove_field( sp, target_field_type_id, center );
for( const std::pair<const field_type_id, field_entry> &fd : std::get<0>( field_removed ) ) {
if( fd.first.is_valid() && !fd.first.id().is_null() ) {
sp.make_sound( caster.pos() );
if( fd.first.id() == fd_fatigue ) {
handle_remove_fd_fatigue_field( field_removed, caster );
} else {
caster.add_msg_if_player( m_neutral, _( "The %s dissipates." ),
fd.second.get_intensity_level().name );
}
}
}
}
void spell_effect::area_pull( const spell &sp, Creature &caster, const tripoint ¢er )
{
area_expander expander;
expander.max_range = sp.aoe();
expander.run( center );
expander.sort_ascending();
for( const auto &node : expander.area ) {
if( node.from == node.position ) {
continue;
}
spell_move( sp, caster, node.position, node.from );
}
sp.make_sound( caster.pos() );
}
void spell_effect::area_push( const spell &sp, Creature &caster, const tripoint ¢er )
{
area_expander expander;
expander.max_range = sp.aoe();
expander.run( center );
expander.sort_descending();
for( const auto &node : expander.area ) {
if( node.from == node.position ) {
continue;
}
spell_move( sp, caster, node.from, node.position );
}
sp.make_sound( caster.pos() );
}
static void character_push_effects( Creature *caster, Character &guy, tripoint &push_dest,
const int push_distance, const std::vector<tripoint> &push_vec )
{
int dist_left = std::abs( push_distance );
for( const tripoint &pushed_point : push_vec ) {
if( get_map().impassable( pushed_point ) ) {
guy.hurtall( dist_left * 4, caster );
push_dest = pushed_point;
break;
} else {
dist_left--;
}
}
guy.setpos( push_dest );
}
void spell_effect::directed_push( const spell &sp, Creature &caster, const tripoint &target )
{
std::set<tripoint> area = spell_effect_area( sp, target, caster );
// this group of variables is for deferring movement of the avatar
int pushed_distance = 0;
tripoint push_to;
std::vector<tripoint> pushed_vec;
bool player_pushed = false;
::map &here = get_map();
// whether it's push or pull, so how the multimap is sorted
// -1 is push and 1 is pull
const int sign = sp.damage() > 0 ? -1 : 1;
std::multimap<int, tripoint> targets_ordered_by_range;
for( const tripoint &pt : area ) {
targets_ordered_by_range.emplace( sign * rl_dist( pt, caster.pos() ), pt );
}
creature_tracker &creatures = get_creature_tracker();
for( const std::pair<const int, tripoint> &pair : targets_ordered_by_range ) {
const tripoint &push_point = pair.second;
const units::angle angle = coord_to_angle( caster.pos(), target );
// positive is push, negative is pull
int push_distance = sp.damage();
const int prev_distance = rl_dist( caster.pos(), target );
if( push_distance < 0 ) {
push_distance = std::max( -std::abs( push_distance ), -std::abs( prev_distance ) );
}
if( push_distance == 0 ) {
continue;
}
tripoint push_dest;
calc_ray_end( angle, push_distance, push_point, push_dest );
const std::vector<tripoint> push_vec = line_to( push_point, push_dest );
const Creature *critter = creatures.creature_at<Creature>( push_point );
if( critter != nullptr ) {
const Creature::Attitude attitude_to_target =
caster.attitude_to( *creatures.creature_at<Creature>( push_point ) );
monster *mon = creatures.creature_at<monster>( push_point );
npc *guy = creatures.creature_at<npc>( push_point );
if( ( sp.is_valid_target( spell_target::self ) && push_point == caster.pos() ) ||
( attitude_to_target == Creature::Attitude::FRIENDLY &&
sp.is_valid_target( spell_target::ally ) ) ||
( ( attitude_to_target == Creature::Attitude::HOSTILE ||
attitude_to_target == Creature::Attitude::NEUTRAL ) &&
sp.is_valid_target( spell_target::hostile ) ) ) {
if( creatures.creature_at<avatar>( push_point ) ) {
// defer this because this absolutely must be done last in order not to mess up our calculations
player_pushed = true;
pushed_distance = push_distance;
push_to = push_dest;
pushed_vec = push_vec;
} else if( mon ) {
int dist_left = std::abs( push_distance );
for( const tripoint &pushed_push_point : push_vec ) {
if( get_map().impassable( pushed_push_point ) ) {
mon->apply_damage( &caster, bodypart_id(), dist_left * 10 );
push_dest = pushed_push_point;
break;
} else {
dist_left--;
}
}
mon->setpos( push_dest );
} else if( guy ) {
character_push_effects( &caster, *guy, push_dest, push_distance, push_vec );