forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_field.cpp
2329 lines (2151 loc) · 99 KB
/
map_field.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 <bitset>
#include <cmath>
#include <cstddef>
#include <functional>
#include <iosfwd>
#include <list>
#include <map>
#include <memory>
#include <new>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "bodypart.h"
#include "calendar.h"
#include "cata_utility.h"
#include "character.h"
#include "colony.h"
#include "coordinate_conversions.h"
#include "coordinates.h"
#include "creature.h"
#include "creature_tracker.h"
#include "damage.h"
#include "debug.h"
#include "effect.h"
#include "emit.h"
#include "enums.h"
#include "field.h"
#include "field_type.h"
#include "fire.h"
#include "fungal_effects.h"
#include "game.h"
#include "game_constants.h"
#include "item.h"
#include "itype.h"
#include "level_cache.h"
#include "line.h"
#include "make_static.h"
#include "map.h"
#include "map_field.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "material.h"
#include "messages.h"
#include "mongroup.h"
#include "monster.h"
#include "mtype.h"
#include "npc.h"
#include "optional.h"
#include "overmapbuffer.h"
#include "point.h"
#include "rng.h"
#include "scent_block.h"
#include "scent_map.h"
#include "submap.h"
#include "teleport.h"
#include "translations.h"
#include "type_id.h"
#include "units.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "weather.h"
static const efftype_id effect_badpoison( "badpoison" );
static const efftype_id effect_blind( "blind" );
static const efftype_id effect_corroding( "corroding" );
static const efftype_id effect_fungus( "fungus" );
static const efftype_id effect_onfire( "onfire" );
static const efftype_id effect_poison( "poison" );
static const efftype_id effect_stung( "stung" );
static const efftype_id effect_stunned( "stunned" );
static const efftype_id effect_teargas( "teargas" );
static const itype_id itype_rm13_armor_on( "rm13_armor_on" );
static const itype_id itype_rock( "rock" );
static const json_character_flag json_flag_HEATSINK( "HEATSINK" );
static const material_id material_iflesh( "iflesh" );
static const material_id material_veggy( "veggy" );
static const species_id species_FUNGUS( "FUNGUS" );
static const trait_id trait_ACIDPROOF( "ACIDPROOF" );
static const trait_id trait_ELECTRORECEPTORS( "ELECTRORECEPTORS" );
static const trait_id trait_GASTROPOD_FOOT( "GASTROPOD_FOOT" );
static const trait_id trait_M_IMMUNE( "M_IMMUNE" );
static const trait_id trait_M_SKIN2( "M_SKIN2" );
static const trait_id trait_M_SKIN3( "M_SKIN3" );
static const trait_id trait_THRESH_INSECT( "THRESH_INSECT" );
static const trait_id trait_THRESH_MARLOSS( "THRESH_MARLOSS" );
static const trait_id trait_THRESH_MYCUS( "THRESH_MYCUS" );
static const trait_id trait_THRESH_SPIDER( "THRESH_SPIDER" );
using namespace map_field_processing;
void map::create_burnproducts( const tripoint &p, const item &fuel, const units::mass &burned_mass )
{
const std::map<material_id, int> all_mats = fuel.made_of();
if( all_mats.empty() ) {
return;
}
const units::mass by_weight = burned_mass;
const float mat_total = fuel.type->mat_portion_total == 0 ? 1 : fuel.type->mat_portion_total;
for( const auto &mat : all_mats ) {
for( const auto &bp : mat.first->burn_products() ) {
itype_id id = bp.first;
// Spawning the same item as the one that was just burned is pointless
// and leads to infinite recursion.
if( fuel.typeId() == id ) {
continue;
}
const float eff = bp.second;
// distribute byproducts by weight AND portion of item
const int n = std::floor( eff * ( by_weight / item::find_type( id )->weight ) *
( mat.second / mat_total ) );
if( n <= 0 ) {
continue;
}
spawn_item( p, id, n, 1, calendar::turn );
}
}
}
// Use a helper for a bit less boilerplate
int map::burn_body_part( Character &you, field_entry &cur, const bodypart_id &bp, const int scale )
{
int total_damage = 0;
const int intensity = cur.get_field_intensity();
const int damage = rng( 1, ( scale + intensity ) / 2 );
// A bit ugly, but better than being annoyed by acid when in hazmat
if( you.get_armor_type( damage_type::ACID, bp ) < damage ) {
const dealt_damage_instance ddi = you.deal_damage( nullptr, bp, damage_instance( damage_type::ACID,
damage ) );
total_damage += ddi.total_damage();
}
// Represents acid seeping in rather than being splashed on
you.add_env_effect( effect_corroding, bp, 2 + intensity, time_duration::from_turns( rng( 2,
1 + intensity ) ), bp, false, 0 );
return total_damage;
}
void map::process_fields()
{
for( int z = -OVERMAP_DEPTH; z <= OVERMAP_HEIGHT; z++ ) {
auto &field_cache = get_cache( z ).field_cache;
for( int x = 0; x < my_MAPSIZE; x++ ) {
for( int y = 0; y < my_MAPSIZE; y++ ) {
if( field_cache[ x + y * MAPSIZE ] ) {
submap *const current_submap = get_submap_at_grid( { x, y, z } );
if( current_submap == nullptr ) {
debugmsg( "Tried to process field at (%d,%d,%d) but the submap is not loaded", x, y, z );
continue;
}
process_fields_in_submap( current_submap, tripoint( x, y, z ) );
if( current_submap->field_count == 0 ) {
field_cache[ x + y * MAPSIZE ] = false;
}
}
}
}
}
}
bool ter_furn_has_flag( const ter_t &ter, const furn_t &furn, const ter_furn_flag flag )
{
return ter.has_flag( flag ) || furn.has_flag( flag );
}
static int ter_furn_movecost( const ter_t &ter, const furn_t &furn )
{
if( ter.movecost == 0 ) {
return 0;
}
if( furn.movecost < 0 ) {
return 0;
}
return ter.movecost + furn.movecost;
}
// Wrapper to allow skipping bound checks except at the edges of the map
std::pair<tripoint, maptile> map::maptile_has_bounds( const tripoint &p, const bool bounds_checked )
{
if( bounds_checked ) {
// We know that the point is in bounds
return {p, maptile_at_internal( p )};
}
return {p, maptile_at( p )};
}
std::array<std::pair<tripoint, maptile>, 8> map::get_neighbors( const tripoint &p )
{
// Find out which edges are in the bubble
// Where possible, do just one bounds check for all the neighbors
const bool west = p.x > 0;
const bool north = p.y > 0;
const bool east = p.x < SEEX * my_MAPSIZE - 1;
const bool south = p.y < SEEY * my_MAPSIZE - 1;
return std::array< std::pair<tripoint, maptile>, 8 > { {
maptile_has_bounds( p + eight_horizontal_neighbors[0], west &&north ),
maptile_has_bounds( p + eight_horizontal_neighbors[1], north ),
maptile_has_bounds( p + eight_horizontal_neighbors[2], east &&north ),
maptile_has_bounds( p + eight_horizontal_neighbors[3], west ),
maptile_has_bounds( p + eight_horizontal_neighbors[4], east ),
maptile_has_bounds( p + eight_horizontal_neighbors[5], west &&south ),
maptile_has_bounds( p + eight_horizontal_neighbors[6], south ),
maptile_has_bounds( p + eight_horizontal_neighbors[7], east &&south ),
}
};
}
bool map::gas_can_spread_to( field_entry &cur, const maptile &dst )
{
const field_entry *tmpfld = dst.get_field().find_field( cur.get_field_type() );
// Candidates are existing weaker fields or navigable/flagged tiles with no field.
if( tmpfld == nullptr || tmpfld->get_field_intensity() < cur.get_field_intensity() ) {
const ter_t &ter = dst.get_ter_t();
const furn_t &frn = dst.get_furn_t();
return ter_furn_movecost( ter, frn ) > 0 ||
ter_furn_has_flag( ter, frn, ter_furn_flag::TFLAG_PERMEABLE );
}
return false;
}
void map::gas_spread_to( field_entry &cur, maptile &dst, const tripoint &p )
{
const field_type_id current_type = cur.get_field_type();
const time_duration current_age = cur.get_field_age();
const int current_intensity = cur.get_field_intensity();
field_entry *f = dst.find_field( current_type );
// Nearby gas grows thicker, and ages are shared.
const time_duration age_fraction = current_age / current_intensity;
if( f != nullptr ) {
f->set_field_intensity( f->get_field_intensity() + 1 );
cur.set_field_intensity( current_intensity - 1 );
f->set_field_age( f->get_field_age() + age_fraction );
cur.set_field_age( current_age - age_fraction );
// Or, just create a new field.
} else if( add_field( p, current_type, 1, 0_turns ) ) {
f = dst.find_field( current_type );
if( f != nullptr ) {
f->set_field_age( age_fraction );
} else {
debugmsg( "While spreading the gas, field was added but doesn't exist." );
}
cur.set_field_intensity( current_intensity - 1 );
cur.set_field_age( current_age - age_fraction );
}
}
void map::spread_gas( field_entry &cur, const tripoint &p, int percent_spread,
const time_duration &outdoor_age_speedup, scent_block &sblk, const oter_id &om_ter )
{
// TODO: fix point types
const bool sheltered = g->is_sheltered( p );
weather_manager &weather = get_weather();
const int winddirection = weather.winddirection;
const int windpower = get_local_windpower( weather.windspeed, om_ter, p, winddirection,
sheltered );
const int current_intensity = cur.get_field_intensity();
const field_type_id ft_id = cur.get_field_type();
const int scent_neutralize = ft_id->get_intensity_level( current_intensity -
1 ).scent_neutralization;
if( scent_neutralize > 0 ) {
// modify scents by neutralization value (minus)
for( const tripoint &tmp : points_in_radius( p, 1 ) ) {
sblk.apply_gas( tmp, scent_neutralize );
}
}
// Dissipate faster outdoors.
if( is_outside( p ) ) {
const time_duration current_age = cur.get_field_age();
cur.set_field_age( current_age + outdoor_age_speedup );
}
// Bail out if we don't meet the spread chance or required intensity.
if( current_intensity <= 1 || rng( 1, 100 - windpower ) > percent_spread ) {
return;
}
// First check if we can fall
// TODO: Make fall and rise chances parameters to enable heavy/light gas
if( p.z > -OVERMAP_DEPTH ) {
const tripoint down{ p.xy(), p.z - 1 };
maptile down_tile = maptile_at_internal( down );
if( gas_can_spread_to( cur, down_tile ) && valid_move( p, down, true, true ) ) {
gas_spread_to( cur, down_tile, down );
return;
}
}
auto neighs = get_neighbors( p );
size_t end_it = static_cast<size_t>( rng( 0, neighs.size() - 1 ) );
std::vector<size_t> spread;
// Then, spread to a nearby point.
// If not possible (or randomly), try to spread up
// Wind direction will block the field spreading into the wind.
// Start at end_it + 1, then wrap around until all elements have been processed.
for( size_t i = ( end_it + 1 ) % neighs.size(), count = 0;
count != neighs.size();
i = ( i + 1 ) % neighs.size(), count++ ) {
const auto &neigh = neighs[i];
if( gas_can_spread_to( cur, neigh.second ) ) {
spread.push_back( i );
}
}
if( !spread.empty() && one_in( spread.size() ) ) {
// Construct the destination from offset and p
if( sheltered || windpower < 5 ) {
std::pair<tripoint, maptile> &n = neighs[ random_entry( spread ) ];
gas_spread_to( cur, n.second, n.first );
} else {
std::vector<size_t> neighbour_vec;
auto maptiles = get_wind_blockers( winddirection, p );
// Three map tiles that are facing the wind direction.
const maptile &remove_tile = std::get<0>( maptiles );
const maptile &remove_tile2 = std::get<1>( maptiles );
const maptile &remove_tile3 = std::get<2>( maptiles );
for( const size_t &i : spread ) {
const maptile &neigh = neighs[i].second;
if( ( neigh.pos_ != remove_tile.pos_ &&
neigh.pos_ != remove_tile2.pos_ &&
neigh.pos_ != remove_tile3.pos_ ) ||
x_in_y( 1, std::max( 2, windpower ) ) ) {
neighbour_vec.push_back( i );
}
}
if( !neighbour_vec.empty() ) {
std::pair<tripoint, maptile> &n = neighs[ random_entry( neighbour_vec ) ];
gas_spread_to( cur, n.second, n.first );
}
}
} else if( p.z < OVERMAP_HEIGHT ) {
const tripoint up{ p.xy(), p.z + 1 };
maptile up_tile = maptile_at_internal( up );
if( gas_can_spread_to( cur, up_tile ) && valid_move( p, up, true, true ) ) {
gas_spread_to( cur, up_tile, up );
}
}
}
/*
Helper function that encapsulates the logic involved in creating hot air.
*/
void map::create_hot_air( const tripoint &p, int intensity )
{
field_type_id hot_air;
switch( intensity ) {
case 1:
hot_air = fd_hot_air1;
break;
case 2:
hot_air = fd_hot_air2;
break;
case 3:
hot_air = fd_hot_air3;
break;
case 4:
hot_air = fd_hot_air4;
break;
default:
debugmsg( "Tried to spread hot air with intensity %d", intensity );
return;
}
for( int counter = 0; counter < 5; counter++ ) {
tripoint dst( p + point( rng( -1, 1 ), rng( -1, 1 ) ) );
add_field( dst, hot_air, 1 );
}
}
/**
* Common field processing data: shared per submap and field_type.
* Note: maptile here is mutable and is modified for each point in the submap scanning loop.
*/
struct field_proc_data {
scent_block &sblk;
Character &player_character;
const oter_id &om_ter;
map &here;
maptile &map_tile;
field_type_id cur_fd_type_id;
field_type const *cur_fd_type;
};
/*
Function: process_fields_in_submap
Iterates over every field on every tile of the given submap given as parameter.
This is the general update function for field effects. This should only be called once per game turn.
If you need to insert a new field behavior per unit time add a case statement in the switch below.
*/
void map::process_fields_in_submap( submap *const current_submap,
const tripoint &submap )
{
const oter_id &om_ter = overmap_buffer.ter( tripoint_abs_omt( sm_to_omt_copy( submap ) ) );
Character &player_character = get_player_character();
scent_block sblk( submap, get_scent() );
// Initialize the map tile wrapper
maptile map_tile( current_submap, point_zero );
int &locx = map_tile.pos_.x;
int &locy = map_tile.pos_.y;
const point sm_offset = sm_to_ms_copy( submap.xy() );
field_proc_data pd{
sblk,
player_character,
om_ter,
*this,
map_tile,
fd_null,
&( *fd_null )
};
// Loop through all tiles in this submap indicated by current_submap
for( locx = 0; locx < SEEX; locx++ ) {
for( locy = 0; locy < SEEY; locy++ ) {
// Get a reference to the field variable from the submap;
// contains all the pointers to the real field effects.
field &curfield = current_submap->get_field( {static_cast<int>( locx ), static_cast<int>( locy )} );
// when displayed_field_type == fd_null it means that `curfield` has no fields inside
// avoids instantiating (relatively) expensive map iterator
if( !curfield.displayed_field_type() ) {
continue;
}
// This is a translation from local coordinates to submap coordinates.
const tripoint p = tripoint( map_tile.pos() + sm_offset, submap.z );
for( auto it = curfield.begin(); it != curfield.end(); ) {
// Iterating through all field effects in the submap's field.
field_entry &cur = it->second;
const int prev_intensity = cur.is_field_alive() ? cur.get_field_intensity() : 0;
pd.cur_fd_type_id = cur.get_field_type();
pd.cur_fd_type = &( *pd.cur_fd_type_id );
// The field might have been killed by processing a neighbor field
if( prev_intensity == 0 ) {
on_field_modified( p, *pd.cur_fd_type );
--current_submap->field_count;
curfield.remove_field( it++ );
continue;
}
// Don't process "newborn" fields. This gives the player time to run if they need to.
if( cur.get_field_age() == 0_turns ) {
cur.do_decay();
if( !cur.is_field_alive() || cur.get_field_intensity() != prev_intensity ) {
on_field_modified( p, *pd.cur_fd_type );
}
it++;
continue;
}
for( const FieldProcessorPtr &proc : pd.cur_fd_type->get_processors() ) {
proc( p, cur, pd );
}
cur.do_decay();
if( !cur.is_field_alive() || cur.get_field_intensity() != prev_intensity ) {
on_field_modified( p, *pd.cur_fd_type );
}
it++;
}
}
}
sblk.commit_modifications();
}
static void field_processor_upgrade_intensity( const tripoint &, field_entry &cur,
field_proc_data & )
{
// Upgrade field intensity
const field_intensity_level &int_level = cur.get_intensity_level();
if( int_level.intensity_upgrade_chance > 0 &&
int_level.intensity_upgrade_duration > 0_turns &&
calendar::once_every( int_level.intensity_upgrade_duration ) &&
one_in( int_level.intensity_upgrade_chance ) ) {
cur.mod_field_intensity( 1 );
}
}
static void field_processor_underwater_dissipation( const tripoint &, field_entry &cur,
field_proc_data &pd )
{
// Dissipate faster in water
if( pd.map_tile.get_ter_t().has_flag( ter_furn_flag::TFLAG_SWIMMABLE ) ) {
cur.mod_field_age( pd.cur_fd_type->underwater_age_speedup );
}
}
static void field_processor_fd_acid( const tripoint &p, field_entry &cur, field_proc_data &pd )
{
//cur_fd_type_id == fd_acid
if( p.z <= -OVERMAP_DEPTH ) {
return;
}
// Try to fall by a z-level
tripoint dst{ p.xy(), p.z - 1 };
if( pd.here.valid_move( p, dst, true, true ) ) {
field_entry *acid_there = pd.here.get_field( dst, fd_acid );
if( !acid_there ) {
pd.here.add_field( dst, fd_acid, cur.get_field_intensity(), cur.get_field_age() );
} else {
// Math can be a bit off,
// but "boiling" falling acid can be allowed to be stronger
// than acid that just lies there
const int sum_intensity = cur.get_field_intensity() + acid_there->get_field_intensity();
const int new_intensity = std::min( 3, sum_intensity );
// No way to get precise elapsed time, let's always reset
// Allow falling acid to last longer than regular acid to show it off
const time_duration new_age = -1_minutes * ( sum_intensity - new_intensity );
acid_there->set_field_intensity( new_intensity );
acid_there->set_field_age( new_age );
}
// Set ourselves up for removal
cur.set_field_intensity( 0 );
}
// TODO: Allow spreading to the sides if age < 0 && intensity == 3
}
static void field_processor_fd_extinguisher( const tripoint &p, field_entry &cur,
field_proc_data &pd )
{
// if( cur_fd_type_id == fd_extinguisher )
if( field_entry *fire_here = pd.map_tile.find_field( fd_fire ) ) {
// extinguisher fights fire in 1:1 ratio
const int min_int = std::min( fire_here->get_field_intensity(), cur.get_field_intensity() );
pd.here.mod_field_intensity( p, fd_fire, -min_int );
cur.mod_field_intensity( -min_int );
}
}
static void field_processor_apply_slime( const tripoint &p, field_entry &cur, field_proc_data &pd )
{
// if( cur_fd_type.apply_slime_factor > 0 )
pd.sblk.apply_slime( p, cur.get_field_intensity() * pd.cur_fd_type->apply_slime_factor );
}
// Spread gaseous fields
void field_processor_spread_gas( const tripoint &p, field_entry &cur, field_proc_data &pd )
{
// if( cur.gas_can_spread() )
pd.here.spread_gas( cur, p, pd.cur_fd_type->percent_spread, pd.cur_fd_type->outdoor_age_speedup,
pd.sblk, pd.om_ter );
}
static void field_processor_fd_fungal_haze( const tripoint &p, field_entry &cur,
field_proc_data &/*pd*/ )
{
// if( cur_fd_type_id == fd_fungal_haze ) {
if( one_in( 10 - 2 * cur.get_field_intensity() ) ) {
// Haze'd terrain
fungal_effects().spread_fungus( p );
}
}
// Process npc complaints moved to player_in_field
static void field_processor_extra_radiation( const tripoint &p, field_entry &cur,
field_proc_data &pd )
{
// Apply radiation
const field_intensity_level &ilevel = cur.get_intensity_level();
if( ilevel.extra_radiation_max > 0 ) {
int extra_radiation = rng( ilevel.extra_radiation_min, ilevel.extra_radiation_max );
pd.here.adjust_radiation( p, extra_radiation );
}
}
void field_processor_wandering_field( const tripoint &p, field_entry &cur, field_proc_data &pd )
{
// Apply wandering fields from vents
const field_type_id wandering_field_type_id = pd.cur_fd_type->wandering_field;
for( const tripoint &pnt : points_in_radius( p, cur.get_field_intensity() - 1 ) ) {
field &wandering_field = pd.here.get_field( pnt );
field_entry *tmpfld = wandering_field.find_field( wandering_field_type_id );
if( tmpfld && tmpfld->get_field_intensity() < cur.get_field_intensity() ) {
pd.here.mod_field_intensity( pnt, wandering_field_type_id, 1 );
} else {
pd.here.add_field( pnt, wandering_field_type_id, cur.get_field_intensity() );
}
}
}
void field_processor_fd_fire_vent( const tripoint &p, field_entry &cur, field_proc_data &pd )
{
if( cur.get_field_intensity() > 1 ) {
if( one_in( 3 ) ) {
cur.set_field_intensity( cur.get_field_intensity() - 1 );
}
pd.here.create_hot_air( p, cur.get_field_intensity() );
} else {
pd.here.add_field( p, fd_flame_burst, 3, cur.get_field_age() );
cur.set_field_intensity( 0 );
}
}
//TODO extract common logic from this and field_processor_fd_fire_vent
void field_processor_fd_flame_burst( const tripoint &p, field_entry &cur, field_proc_data &pd )
{
if( cur.get_field_intensity() > 1 ) {
cur.set_field_intensity( cur.get_field_intensity() - 1 );
pd.here.create_hot_air( p, cur.get_field_intensity() );
} else {
pd.here.add_field( p, fd_fire_vent, 3, cur.get_field_age() );
cur.set_field_intensity( 0 );
}
}
static void field_processor_fd_electricity( const tripoint &p, field_entry &cur,
field_proc_data &pd )
{
// Higher chance of spreading for intense fields
int current_intensity = cur.get_field_intensity();
if( one_in( current_intensity * 2 ) ) {
return;
}
const int spread_intensity_cap = 3 + std::max( ( current_intensity - 3 ) / 2, 0 );
std::vector<tripoint> grounded_tiles;
std::vector<tripoint> tiles_with_creatures;
std::vector<tripoint> other_tiles;
bool valid_candidates = false;
for( const tripoint &dst : points_in_radius( p, 1 ) ) {
// Skip tiles with intense fields
const field_type_str_id &field_type = pd.here.get_applicable_electricity_field( dst );
if( field_entry *field = pd.here.get_field( dst, field_type ) ) {
if( field->get_field_intensity() >= spread_intensity_cap ) {
continue;
}
}
if( pd.here.impassable( dst ) ) {
grounded_tiles.push_back( dst );
} else {
if( get_creature_tracker().creature_at( dst ) ) {
tiles_with_creatures.push_back( dst );
} else {
other_tiles.push_back( dst );
}
}
valid_candidates = true;
}
if( !valid_candidates ) {
return;
}
const bool here_impassable = pd.here.impassable( p );
int grounded_weight = here_impassable ? 1 : 6;
int creature_weight = here_impassable ? 5 : 6;
int other_weight = here_impassable ? 0 : 1;
std::vector<tripoint> *target_vector = nullptr;
while( current_intensity > 0 ) {
if( here_impassable && one_in( 3 ) ) {
// Electricity in impassable tiles will find a way to the ground sometimes
cur.set_field_intensity( --current_intensity );
continue;
}
const int vector_choice = bucket_index_from_weight_list( std::vector<int>( {
grounded_tiles.empty() ? 0 : grounded_weight,
tiles_with_creatures.empty() ? 0 : creature_weight,
other_tiles.empty() ? 0 : other_weight
} ) );
switch( vector_choice ) {
default:
case 0:
if( here_impassable && !one_in( 5 ) ) {
return;
}
target_vector = &grounded_tiles;
break;
case 1:
target_vector = &tiles_with_creatures;
break;
case 2:
target_vector = &other_tiles;
break;
}
if( target_vector->empty() ) {
return;
}
int vector_index = rng( 0, target_vector->size() - 1 );
auto target_it = target_vector->begin() + vector_index;
tripoint target_point = *target_it;
const field_type_str_id &field_type = pd.here.get_applicable_electricity_field( target_point );
// Intensify target field if it exists, create a new one otherwise
if( field_entry *target_field = pd.here.get_field( target_point, field_type ) ) {
int target_field_intensity = target_field->get_field_intensity();
target_field->set_field_intensity( ++target_field_intensity );
if( target_field_intensity >= spread_intensity_cap ) {
target_vector->erase( target_it );
}
} else {
pd.here.add_field( target_point, field_type, 1, cur.get_field_age() + 1_turns );
}
cur.set_field_intensity( --current_intensity );
if( one_in( current_intensity * 2 ) ) {
// Weaker fields have a harder time spreading
break;
}
}
}
static void field_processor_monster_spawn( const tripoint &p, field_entry &cur,
field_proc_data &pd )
{
const field_intensity_level &int_level = cur.get_intensity_level();
int monster_spawn_chance = int_level.monster_spawn_chance;
int monster_spawn_count = int_level.monster_spawn_count;
if( monster_spawn_count > 0 && monster_spawn_chance > 0 && one_in( monster_spawn_chance ) ) {
for( ; monster_spawn_count > 0; monster_spawn_count-- ) {
std::vector<MonsterGroupResult> spawn_details =
MonsterGroupManager::GetResultFromGroup( int_level.monster_spawn_group, &monster_spawn_count );
for( const MonsterGroupResult &mgr : spawn_details ) {
if( !mgr.name ) {
continue;
}
if( const cata::optional<tripoint> spawn_point =
random_point( points_in_radius( p, int_level.monster_spawn_radius ),
[&pd]( const tripoint & n ) {
return pd.here.passable( n );
} ) ) {
pd.here.add_spawn( mgr, *spawn_point );
}
}
}
}
}
static void field_processor_fd_push_items( const tripoint &p, field_entry &, field_proc_data &pd )
{
map_stack items = pd.here.i_at( p );
creature_tracker &creatures = get_creature_tracker();
for( auto pushee = items.begin(); pushee != items.end(); ) {
if( pushee->typeId() != itype_rock ||
pushee->age() < 1_turns ) {
pushee++;
} else {
std::vector<tripoint> valid;
for( const tripoint &dst : points_in_radius( p, 1 ) ) {
if( dst != p && pd.here.get_field( dst, fd_push_items ) ) {
valid.push_back( dst );
}
}
if( !valid.empty() ) {
item tmp = *pushee;
tmp.set_age( 0_turns );
pushee = items.erase( pushee );
tripoint newp = random_entry( valid );
pd.here.add_item_or_charges( newp, tmp );
if( pd.player_character.pos() == newp ) {
add_msg( m_bad, _( "A %s hits you!" ), tmp.tname() );
const bodypart_id hit = pd.player_character.get_random_body_part();
pd.player_character.deal_damage( nullptr, hit, damage_instance( damage_type::BASH, 6 ) );
pd.player_character.check_dead_state();
}
if( npc *const n = creatures.creature_at<npc>( newp ) ) {
// TODO: combine with player character code above
const bodypart_id hit = pd.player_character.get_random_body_part();
n->deal_damage( nullptr, hit, damage_instance( damage_type::BASH, 6 ) );
add_msg_if_player_sees( newp, _( "A %1$s hits %2$s!" ), tmp.tname(), n->get_name() );
n->check_dead_state();
} else if( monster *const mon = creatures.creature_at<monster>( newp ) ) {
mon->apply_damage( nullptr, bodypart_id( "torso" ),
6 - mon->get_armor_bash( bodypart_id( "torso" ) ) );
add_msg_if_player_sees( newp, _( "A %1$s hits the %2$s!" ), tmp.tname(), mon->name() );
mon->check_dead_state();
}
} else {
pushee++;
}
}
}
}
static void field_processor_fd_shock_vent( const tripoint &p, field_entry &cur,
field_proc_data &pd )
{
if( cur.get_field_intensity() > 1 ) {
if( one_in( 5 ) ) {
cur.set_field_intensity( cur.get_field_intensity() - 1 );
}
} else {
cur.set_field_intensity( 3 );
int num_bolts = rng( 3, 6 );
for( int i = 0; i < num_bolts; i++ ) {
point dir;
while( dir == point_zero ) {
dir = { rng( -1, 1 ), rng( -1, 1 ) };
}
int dist = rng( 4, 12 );
point bolt = p.xy();
for( int n = 0; n < dist; n++ ) {
bolt += dir;
pd.here.add_field( tripoint( bolt, p.z ), fd_electricity, rng( 2, 3 ) );
if( one_in( 4 ) ) {
if( dir.x == 0 ) {
dir.x = rng( 0, 1 ) * 2 - 1;
} else {
dir.x = 0;
}
}
if( one_in( 4 ) ) {
if( dir.y == 0 ) {
dir.y = rng( 0, 1 ) * 2 - 1;
} else {
dir.y = 0;
}
}
}
}
}
}
static void field_processor_fd_acid_vent( const tripoint &p, field_entry &cur, field_proc_data &pd )
{
if( cur.get_field_intensity() > 1 ) {
if( cur.get_field_age() >= 1_minutes ) {
cur.set_field_intensity( cur.get_field_intensity() - 1 );
cur.set_field_age( 0_turns );
}
} else {
cur.set_field_intensity( 3 );
for( const tripoint &t : points_in_radius( p, 5 ) ) {
const field_entry *acid = pd.here.get_field( t, fd_acid );
if( acid && acid->get_field_intensity() == 0 ) {
int new_intensity = 3 - rl_dist( p, t ) / 2 + ( one_in( 3 ) ? 1 : 0 );
if( new_intensity > 3 ) {
new_intensity = 3;
}
if( new_intensity > 0 ) {
pd.here.add_field( t, fd_acid, new_intensity );
}
}
}
}
}
void field_processor_fd_bees( const tripoint &p, field_entry &cur, field_proc_data &pd )
{
// Poor bees are vulnerable to so many other fields.
// TODO: maybe adjust effects based on different fields.
// FIXME replace this insanity with a bool flag in the field_type
const field &curfield = pd.here.field_at( p );
if( curfield.find_field( fd_web ) ||
curfield.find_field( fd_fire ) ||
curfield.find_field( fd_smoke ) ||
curfield.find_field( fd_toxic_gas ) ||
curfield.find_field( fd_tear_gas ) ||
curfield.find_field( fd_relax_gas ) ||
curfield.find_field( fd_nuke_gas ) ||
curfield.find_field( fd_gas_vent ) ||
curfield.find_field( fd_smoke_vent ) ||
curfield.find_field( fd_fungicidal_gas ) ||
curfield.find_field( fd_insecticidal_gas ) ||
curfield.find_field( fd_fire_vent ) ||
curfield.find_field( fd_flame_burst ) ||
curfield.find_field( fd_electricity ) ||
curfield.find_field( fd_fatigue ) ||
curfield.find_field( fd_shock_vent ) ||
curfield.find_field( fd_plasma ) ||
curfield.find_field( fd_laser ) ||
curfield.find_field( fd_dazzling ) ||
curfield.find_field( fd_incendiary ) ) {
// Kill them at the end of processing.
cur.set_field_intensity( 0 );
} else {
// Bees chase the player if in range, wander randomly otherwise.
if( !pd.player_character.is_underwater() &&
rl_dist( p, pd.player_character.pos() ) < 10 &&
pd.here.clear_path( p, pd.player_character.pos(), 10, 1, 100 ) ) {
std::vector<point> candidate_positions =
squares_in_direction( p.xy(), pd.player_character.pos().xy() );
for( const point &candidate_position : candidate_positions ) {
field &target_field = pd.here.get_field( tripoint( candidate_position, p.z ) );
// Only shift if there are no bees already there.
// TODO: Figure out a way to merge bee fields without allowing
// Them to effectively move several times in a turn depending
// on iteration direction.
if( !target_field.find_field( fd_bees ) ) {
pd.here.add_field( tripoint( candidate_position, p.z ), fd_bees,
cur.get_field_intensity(), cur.get_field_age() );
cur.set_field_intensity( 0 );
break;
}
}
} else {
pd.here.spread_gas( cur, p, 5, 0_turns, pd.sblk, pd.om_ter );
}
}
}
void field_processor_fd_incendiary( const tripoint &p, field_entry &cur, field_proc_data &pd )
{
// Needed for variable scope
tripoint dst( p + point( rng( -1, 1 ), rng( -1, 1 ) ) );
if( pd.here.has_flag( ter_furn_flag::TFLAG_FLAMMABLE, dst ) ||
pd.here.has_flag( ter_furn_flag::TFLAG_FLAMMABLE_ASH, dst ) ||
pd.here.has_flag( ter_furn_flag::TFLAG_FLAMMABLE_HARD, dst ) ) {
pd.here.add_field( dst, fd_fire, 1 );
}
// Check piles for flammable items and set those on fire
if( pd.here.flammable_items_at( dst ) ) {
pd.here.add_field( dst, fd_fire, 1 );
}
pd.here.create_hot_air( p, cur.get_field_intensity() );
}
static void field_processor_make_rubble( const tripoint &p, field_entry &, field_proc_data &pd )
{
// if( cur_fd_type.legacy_make_rubble )
// Legacy Stuff
pd.here.make_rubble( p );
}
static void field_processor_fd_fungicidal_gas( const tripoint &p, field_entry &cur,
field_proc_data &pd )
{
// Check the terrain and replace it accordingly to simulate the fungus dieing off
const ter_t &ter = pd.map_tile.get_ter_t();
const furn_t &frn = pd.map_tile.get_furn_t();
const int intensity = cur.get_field_intensity();
if( ter.has_flag( ter_furn_flag::TFLAG_FUNGUS ) && one_in( 10 / intensity ) ) {
pd.here.ter_set( p, t_dirt );
}
if( frn.has_flag( ter_furn_flag::TFLAG_FUNGUS ) && one_in( 10 / intensity ) ) {
pd.here.furn_set( p, f_null );
}
}
void field_processor_fd_fire( const tripoint &p, field_entry &cur, field_proc_data &pd )
{
const field_type_id fd_fire = ::fd_fire;
map &here = pd.here;
maptile &map_tile = pd.map_tile;
const oter_id om_ter = pd.om_ter;
cur.set_field_age( std::max( -24_hours, cur.get_field_age() ) );
// Entire objects for ter/frn for flags
bool sheltered = g->is_sheltered( p );
weather_manager &weather = get_weather();
int winddirection = weather.winddirection;
int windpower = get_local_windpower( weather.windspeed, om_ter, p, winddirection,
sheltered );
const ter_t &ter = map_tile.get_ter_t();
const furn_t &frn = map_tile.get_furn_t();
// We've got ter/furn cached, so let's use that
const bool is_sealed = ter_furn_has_flag( ter, frn, ter_furn_flag::TFLAG_SEALED ) &&
!ter_furn_has_flag( ter, frn, ter_furn_flag::TFLAG_ALLOW_FIELD_EFFECT );
// Smoke generation probability, consumed items count
int smoke = 0;
int consumed = 0;
// How much time to add to the fire's life due to burned items/terrain/furniture
time_duration time_added = 0_turns;
// Checks if the fire can spread
const bool can_spread = !ter_furn_has_flag( ter, frn, ter_furn_flag::TFLAG_FIRE_CONTAINER );
// If the flames are in furniture with fire_container flag like brazier or oven,
// they're fully contained, so skip consuming terrain
const bool can_burn = ( ter.is_flammable() || frn.is_flammable() ) &&
!ter_furn_has_flag( ter, frn, ter_furn_flag::TFLAG_FIRE_CONTAINER );
// The huge indent below should probably be somehow moved away from here