forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map_field.cpp
2005 lines (1868 loc) · 96.2 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 <cstddef>
#include <list>
#include <memory>
#include <optional>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
#include "avatar.h"
#include "basecamp.h"
#include "bodypart.h"
#include "calendar.h"
#include "cata_utility.h"
#include "colony.h"
#include "coordinate_conversions.h"
#include "creature.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 "int_id.h"
#include "item.h"
#include "item_contents.h"
#include "itype.h"
#include "line.h"
#include "map.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 "overmapbuffer.h"
#include "player.h"
#include "pldata.h"
#include "point.h"
#include "rng.h"
#include "scent_block.h"
#include "string_id.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 itype_id itype_rm13_armor_on( "rm13_armor_on" );
static const itype_id itype_rock( "rock" );
static const species_id FUNGUS( "FUNGUS" );
static const species_id INSECT( "INSECT" );
static const species_id SPIDER( "SPIDER" );
static const bionic_id bio_heatsink( "bio_heatsink" );
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 efftype_id effect_webbed( "webbed" );
static const std::string flag_FUNGUS( "FUNGUS" );
static const std::string flag_GAS_PROOF( "GAS_PROOF" );
static const trait_id trait_ACIDPROOF( "ACIDPROOF" );
static const trait_id trait_ELECTRORECEPTORS( "ELECTRORECEPTORS" );
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_MARLOSS( "THRESH_MARLOSS" );
static const trait_id trait_THRESH_MYCUS( "THRESH_MYCUS" );
static const trait_id trait_WEB_WALKER( "WEB_WALKER" );
void map::create_burnproducts( const tripoint &p, const item &fuel, const units::mass &burned_mass )
{
std::vector<material_id> all_mats = fuel.made_of();
if( all_mats.empty() ) {
return;
}
// Items that are multiple materials are assumed to be equal parts each.
const units::mass by_weight = burned_mass / all_mats.size();
for( material_id &mat : all_mats ) {
for( auto &bp : mat->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;
const int n = std::floor( eff * ( by_weight / id->weight ) );
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( player &u, field_entry &cur, body_part 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( u.get_armor_type( DT_ACID, convert_bp( bp ) ) < damage ) {
const dealt_damage_instance ddi = u.deal_damage( nullptr, convert_bp( bp ).id(),
damage_instance( DT_ACID, damage ) );
total_damage += ddi.total_damage();
}
// Represents acid seeping in rather than being splashed on
u.add_env_effect( effect_corroding, bp, 2 + intensity, time_duration::from_turns( rng( 2,
1 + intensity ) ), bp, 0 );
return total_damage;
}
void map::process_fields()
{
const int minz = zlevels ? -OVERMAP_DEPTH : abs_sub.z;
const int maxz = zlevels ? OVERMAP_HEIGHT : abs_sub.z;
for( int z = minz; z <= maxz; 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 } );
process_fields_in_submap( current_submap, tripoint( x, y, z ) );
}
}
}
// no need to invalidate "transparency" and "seen" caches here
// they are invalidated point by point inside the `process_fields_in_submap`
}
}
bool ter_furn_has_flag( const ter_t &ter, const furn_t &furn, const ter_bitflags 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 tripoint &src, const tripoint &dst )
{
maptile dst_tile = maptile_at( dst );
const field_entry *tmpfld = dst_tile.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_tile.get_ter_t();
const furn_t &frn = dst_tile.get_furn_t();
return !obstructed_by_vehicle_rotation( src, dst ) &&
( ter_furn_movecost( ter, frn ) > 0 || ter_furn_has_flag( ter, frn, 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 )
{
map &here = get_map();
// TODO: fix point types
const oter_id &cur_om_ter =
overmap_buffer.ter( tripoint_abs_omt( ms_to_omt_copy( here.getabs( p ) ) ) );
const bool sheltered = g->is_sheltered( p );
const weather_manager &weather = get_weather();
const int winddirection = weather.winddirection;
const int windpower = get_local_windpower( weather.windspeed, cur_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( zlevels && p.z > -OVERMAP_DEPTH ) {
const tripoint down{ p.xy(), p.z - 1 };
if( gas_can_spread_to( cur, p, down ) && valid_move( p, down, true, true ) ) {
maptile down_tile = maptile_at_internal( down );
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;
std::vector<size_t> neighbour_vec;
// 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, p, neigh.first ) ) {
spread.push_back( i );
}
}
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 );
if( !spread.empty() && ( !zlevels || one_in( spread.size() ) ) ) {
// Construct the destination from offset and p
if( g->is_sheltered( p ) || windpower < 5 ) {
std::pair<tripoint, maptile> &n = neighs[ random_entry( spread ) ];
gas_spread_to( cur, n.second, n.first );
} else {
end_it = static_cast<size_t>( rng( 0, neighs.size() - 1 ) );
// 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].second;
if( ( neigh.pos_.x != remove_tile.pos_.x && neigh.pos_.y != remove_tile.pos_.y ) ||
( neigh.pos_.x != remove_tile2.pos_.x && neigh.pos_.y != remove_tile2.pos_.y ) ||
( neigh.pos_.x != remove_tile3.pos_.x && neigh.pos_.y != remove_tile3.pos_.y ) ) {
neighbour_vec.push_back( i );
} else if( x_in_y( 1, std::max( 2, windpower ) ) ) {
neighbour_vec.push_back( i );
}
}
if( !neighbour_vec.empty() ) {
std::pair<tripoint, maptile> &n = neighs[neighbour_vec[rng( 0, neighbour_vec.size() - 1 )]];
gas_spread_to( cur, n.second, n.first );
}
}
} else if( zlevels && p.z < OVERMAP_HEIGHT ) {
const tripoint up{ p.xy(), p.z + 1 };
if( gas_can_spread_to( cur, p, up ) && valid_move( p, up, true, true ) ) {
maptile up_tile = maptile_at_internal( up );
gas_spread_to( cur, up_tile, up );
}
}
}
static inline bool check_flammable( const map_data_common_t &t )
{
return t.has_flag( TFLAG_FLAMMABLE ) || t.has_flag( TFLAG_FLAMMABLE_ASH ) ||
t.has_flag( TFLAG_FLAMMABLE_HARD );
}
/*
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 );
}
}
/*
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 )
{
scent_block sblk( submap, g->scent );
// Holds m.field_at(x,y).find_field(fd_some_field) type returns.
// Just to avoid typing that long string for a temp value.
field_entry *tmpfld = nullptr;
map &here = get_map();
tripoint thep;
thep.z = submap.z;
// 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( submap.x * SEEX, submap.y * SEEY );
// 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.
// All submaps are in one long 1d array.
thep.x = locx + sm_offset.x;
thep.y = locy + sm_offset.y;
// A const reference to the tripoint above, so that the code below doesn't accidentally change it
const tripoint &p = thep;
// This should be true only when the field in the current tile changes transparency state,
// More correctly: not just when the field is opaque, but when it changes state
// to a more/less transparent one
bool dirty_transparency_cache = false;
for( auto it = curfield.begin(); it != curfield.end(); ) {
// Iterating through all field effects in the submap's field.
field_entry &cur = it->second;
// Holds cur.get_field_type() as that is what the old system used before rewrite.
field_type_id cur_fd_type_id = cur.get_field_type();
// The field might have been killed by processing a neighbor field
if( !cur.is_field_alive() ) {
if( !cur_fd_type_id->get_transparent( cur.get_field_intensity() - 1 ) ) {
dirty_transparency_cache = true;
}
--current_submap->field_count;
curfield.remove_field( it++ );
continue;
}
// Again, legacy support in the event someone Mods set_field_intensity to allow more values.
if( cur.get_field_intensity() > 3 || cur.get_field_intensity() < 1 ) {
// TODO: Remove this eventually as we would suppoort more than 3 field intensity levels
debugmsg( "Whoooooa intensity of %d", cur.get_field_intensity() );
}
dirty_transparency_cache |= cur_fd_type_id->dirty_transparency_cache;
// Don't process "newborn" fields. This gives the player time to run if they need to.
if( cur.get_field_age() == 0_turns ) {
cur_fd_type_id = fd_null;
}
const field_type &cur_fd_type = *cur_fd_type_id;
// Upgrade field intensity
if( cur.intensity_upgrade_chance() > 0 &&
one_in( cur.intensity_upgrade_chance() ) &&
cur.intensity_upgrade_duration() > 0_turns &&
calendar::once_every( cur.intensity_upgrade_duration() ) ) {
cur.set_field_intensity( cur.get_field_intensity() + 1 );
}
int part;
const ter_t &ter = map_tile.get_ter_t();
// Dissipate faster in water
if( ter.has_flag( TFLAG_SWIMMABLE ) ) {
cur.mod_field_age( cur.get_underwater_age_speedup() );
}
if( cur_fd_type_id == fd_acid ) {
// Try to fall by a z-level
if( zlevels && p.z > -OVERMAP_DEPTH ) {
tripoint dst{ p.xy(), p.z - 1 };
if( valid_move( p, dst, true, true ) ) {
field_entry *acid_there = field_at( dst ).find_field( fd_acid );
if( acid_there == nullptr ) {
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
}
if( cur_fd_type.apply_slime_factor > 0 ) {
sblk.apply_slime( p, cur.get_field_intensity() * cur_fd_type.apply_slime_factor );
}
if( cur_fd_type_id == fd_fire ) {
cur.set_field_age( std::max( -24_hours, cur.get_field_age() ) );
// Entire objects for ter/frn for flags
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, TFLAG_SEALED ) &&
!ter_furn_has_flag( ter, frn, TFLAG_ALLOW_FIELD_EFFECT );
// Consumed items count
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, TFLAG_FIRE_CONTAINER );
const bool no_floor = ter.has_flag( TFLAG_NO_FLOOR );
// 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 = !no_floor && can_spread &&
( check_flammable( ter ) || check_flammable( frn ) );
// The huge indent below should probably be somehow moved away from here
// without forcing the function to use i_at( p ) for fires without items
if( !is_sealed && map_tile.get_item_count() > 0 ) {
map_stack items_here = i_at( p );
std::vector<item> new_content;
for( auto explosive = items_here.begin(); explosive != items_here.end(); ) {
if( explosive->will_explode_in_fire() ) {
// We need to make a copy because the iterator validity is not predictable
item copy = *explosive;
explosive = items_here.erase( explosive );
if( copy.detonate( p, new_content ) ) {
// Need to restart, iterators may not be valid
explosive = items_here.begin();
}
} else {
++explosive;
}
}
fire_data frd( cur.get_field_intensity(), !can_spread );
// The highest # of items this fire can remove in one turn
int max_consume = cur.get_field_intensity() * 2;
for( auto fuel = items_here.begin(); fuel != items_here.end() && consumed < max_consume; ) {
// `item::burn` modifies the charges in order to simulate some of them getting
// destroyed by the fire, this changes the item weight, but may not actually
// destroy it. We need to spawn products anyway.
const units::mass old_weight = fuel->weight( false );
bool destroyed = fuel->burn( frd );
// If the item is considered destroyed, it may have negative charge count,
// see `item::burn?. This in turn means `item::weight` returns a negative value,
// which we can not use, so only call `weight` when it's still an existing item.
const units::mass new_weight = destroyed ? 0_gram : fuel->weight( false );
if( old_weight != new_weight ) {
create_burnproducts( p, *fuel, old_weight - new_weight );
}
if( destroyed ) {
// If we decided the item was destroyed by fire, remove it.
// But remember its contents, except for irremovable mods, if any
const std::list<item *> content_list = fuel->contents.all_items_top();
for( item *it : content_list ) {
if( !it->is_irremovable() ) {
new_content.push_back( item( *it ) );
}
}
fuel = items_here.erase( fuel );
consumed++;
} else {
++fuel;
}
}
spawn_items( p, new_content );
time_added = 1_turns * roll_remainder( frd.fuel_produced );
}
// Get the part of the vehicle in the fire (_internal skips the boundary check)
vehicle *veh = veh_at_internal( p, part );
if( veh != nullptr ) {
veh->damage( part, cur.get_field_intensity() * 10, DT_HEAT, true );
// Damage the vehicle in the fire.
}
if( can_burn ) {
if( ter.has_flag( TFLAG_SWIMMABLE ) ) {
// Flames die quickly on water
cur.set_field_age( cur.get_field_age() + 4_minutes );
}
// Consume the terrain we're on
if( ter_furn_has_flag( ter, frn, TFLAG_FLAMMABLE ) ) {
// The fire feeds on the ground itself until max intensity.
time_added += 1_turns * ( 5 - cur.get_field_intensity() );
if( cur.get_field_intensity() > 1 &&
one_in( 200 - cur.get_field_intensity() * 50 ) ) {
destroy( p, false );
}
} else if( ter_furn_has_flag( ter, frn, TFLAG_FLAMMABLE_HARD ) &&
one_in( 3 ) ) {
// The fire feeds on the ground itself until max intensity.
time_added += 1_turns * ( 4 - cur.get_field_intensity() );
if( cur.get_field_intensity() > 1 &&
one_in( 200 - cur.get_field_intensity() * 50 ) ) {
destroy( p, false );
}
} else if( ter.has_flag( TFLAG_FLAMMABLE_ASH ) ) {
// The fire feeds on the ground itself until max intensity.
time_added += 1_turns * ( 5 - cur.get_field_intensity() );
if( cur.get_field_intensity() > 1 &&
one_in( 200 - cur.get_field_intensity() * 50 ) ) {
if( p.z > 0 ) {
// We're in the air
ter_set( p, t_open_air );
} else {
ter_set( p, t_dirt );
}
}
} else if( frn.has_flag( TFLAG_FLAMMABLE_ASH ) ) {
// The fire feeds on the ground itself until max intensity.
time_added += 1_turns * ( 5 - cur.get_field_intensity() );
if( cur.get_field_intensity() > 1 &&
one_in( 200 - cur.get_field_intensity() * 50 ) ) {
furn_set( p, f_ash );
}
}
}
if( ter.has_flag( TFLAG_NO_FLOOR ) && zlevels && p.z > -OVERMAP_DEPTH ) {
// We're hanging in the air - let's fall down
tripoint dst{ p.xy(), p.z - 1 };
if( valid_move( p, dst, true, true ) ) {
maptile dst_tile = maptile_at_internal( dst );
field_entry *fire_there = dst_tile.find_field( fd_fire );
if( fire_there == nullptr ) {
add_field( dst, fd_fire, 1, 0_turns, false );
cur.set_field_intensity( cur.get_field_intensity() - 1 );
} else {
// Don't fuel raging fires or they'll burn forever
// as they can produce small fires above themselves
int new_intensity = std::max( cur.get_field_intensity(),
fire_there->get_field_intensity() );
// Allow smaller fires to combine
if( new_intensity < 3 &&
cur.get_field_intensity() == fire_there->get_field_intensity() ) {
new_intensity++;
}
// A raging fire below us can support us for a while
// Otherwise decay and decay fast
if( fire_there->get_field_intensity() < 3 || one_in( 10 ) ) {
cur.set_field_intensity( cur.get_field_intensity() - 1 );
}
fire_there->set_field_intensity( new_intensity );
}
break;
}
}
// Lower age is a longer lasting fire
if( time_added != 0_turns ) {
cur.set_field_age( cur.get_field_age() - time_added );
} else if( can_burn ) {
// Nothing to burn = fire should be dying out faster
// Drain more power from big fires, so that they stop raging over nothing
// Except for fires on stoves and fireplaces, those are made to keep the fire alive
cur.mod_field_age( 10_seconds * cur.get_field_intensity() );
}
// Allow raging fires (and only raging fires) to spread up
// Spreading down is achieved by wrecking the walls/floor and then falling
if( zlevels && cur.get_field_intensity() == 3 && p.z < OVERMAP_HEIGHT ) {
const tripoint dst_p = tripoint( p.xy(), p.z + 1 );
// Let it burn through the floor
maptile dst = maptile_at_internal( dst_p );
const auto &dst_ter = dst.get_ter_t();
if( dst_ter.has_flag( TFLAG_NO_FLOOR ) ||
dst_ter.has_flag( TFLAG_FLAMMABLE ) ||
dst_ter.has_flag( TFLAG_FLAMMABLE_ASH ) ||
dst_ter.has_flag( TFLAG_FLAMMABLE_HARD ) ) {
field_entry *nearfire = dst.find_field( fd_fire );
if( nearfire != nullptr ) {
nearfire->mod_field_age( -2_turns );
} else {
add_field( dst_p, fd_fire, 1, 0_turns, false );
}
// Fueling fires above doesn't cost fuel
}
}
// Below we will access our nearest 8 neighbors, so let's cache them now
// This should probably be done more globally, because large fires will re-do it a lot
auto neighs = get_neighbors( p );
// If the flames are in a pit, it can't spread to non-pit
const bool in_pit = can_spread && ter.id.id() == t_pit;
// Count adjacent fires, to optimize out needless smoke and hot air
int adjacent_fires = 0;
// If the flames are big, they contribute to adjacent flames
if( can_spread ) {
if( cur.get_field_intensity() > 1 && one_in( 3 ) ) {
// Basically: Scan around for a spot,
// if there is more fire there, make it bigger and give it some fuel.
// This is how big fires spend their excess age:
// making other fires bigger. Flashpoint.
size_t end_it = static_cast<size_t>( rng( 0, neighs.size() - 1 ) );
for( size_t i = ( end_it + 1 ) % neighs.size(), count = 0;
count != neighs.size() && cur.get_field_age() < 0_turns;
i = ( i + 1 ) % neighs.size(), count++ ) {
maptile &dst = neighs[i].second;
auto dstfld = dst.find_field( fd_fire );
// If the fire exists and is weaker than ours, boost it
if( dstfld != nullptr &&
( dstfld->get_field_intensity() <= cur.get_field_intensity() ||
dstfld->get_field_age() > cur.get_field_age() ) &&
( in_pit == ( dst.get_ter() == t_pit ) ) ) {
if( dstfld->get_field_intensity() < 2 ) {
dstfld->set_field_intensity( dstfld->get_field_intensity() + 1 );
}
dstfld->set_field_age( dstfld->get_field_age() - 5_minutes );
cur.set_field_age( cur.get_field_age() + 5_minutes );
}
if( dstfld != nullptr ) {
adjacent_fires++;
}
}
} else if( cur.get_field_age() < 0_turns && cur.get_field_intensity() < 3 ) {
// See if we can grow into a stage 2/3 fire, for this
// burning neighbors are necessary in addition to
// field age < 0, or alternatively, a LOT of fuel.
// The maximum fire intensity is 1 for a lone fire, 2 for at least 1 neighbor,
// 3 for at least 2 neighbors.
int maximum_intensity = 1;
// The following logic looks a bit complex due to optimization concerns, so here are the semantics:
// 1. Calculate maximum field intensity based on fuel, -50 minutes is 2(medium), -500 minutes is 3(raging)
// 2. Calculate maximum field intensity based on neighbors, 3 neighbors is 2(medium), 7 or more neighbors is 3(raging)
// 3. Pick the higher maximum between 1. and 2.
if( cur.get_field_age() < -500_minutes ) {
maximum_intensity = 3;
} else {
for( auto &neigh : neighs ) {
if( neigh.second.get_field().find_field( fd_fire ) != nullptr ) {
adjacent_fires++;
}
}
maximum_intensity = 1 + ( adjacent_fires >= 3 ) + ( adjacent_fires >= 7 );
if( maximum_intensity < 2 && cur.get_field_age() < -50_minutes ) {
maximum_intensity = 2;
}
}
// If we consumed a lot, the flames grow higher
if( cur.get_field_intensity() < maximum_intensity && cur.get_field_age() < 0_turns ) {
// Fires under 0 age grow in size. Level 3 fires under 0 spread later on.
// Weaken the newly-grown fire
cur.set_field_intensity( cur.get_field_intensity() + 1 );
cur.set_field_age( cur.get_field_age() + 10_minutes * cur.get_field_intensity() );
}
}
// Consume adjacent fuel / terrain / webs to spread.
// Our iterator will start at end_i + 1 and increment from there and then wrap around.
// This guarantees it will check all neighbors, starting from a random one
const size_t end_i = static_cast<size_t>( rng( 0, neighs.size() - 1 ) );
for( size_t i = ( end_i + 1 ) % neighs.size(), count = 0;
count != neighs.size();
i = ( i + 1 ) % neighs.size(), count++ ) {
if( one_in( cur.get_field_intensity() * 2 ) ) {
// Skip some processing to save on CPU
continue;
}
tripoint &dst_p = neighs[i].first;
maptile &dst = neighs[i].second;
// No bounds checking here: we'll treat the invalid neighbors as valid.
// We're using the map tile wrapper, so we can treat invalid tiles as sentinels.
// This will create small oddities on map edges, but nothing more noticeable than
// "cut-off" that happens with bounds checks.
field_entry *nearfire = dst.find_field( fd_fire );
if( nearfire != nullptr ) {
// We handled supporting fires in the section above, no need to do it here
continue;
}
field_entry *nearwebfld = dst.find_field( fd_web );
int spread_chance = 25 * ( cur.get_field_intensity() - 1 );
if( nearwebfld != nullptr ) {
spread_chance = 50 + spread_chance / 2;
}
const ter_t &dster = dst.get_ter_t();
const furn_t &dsfrn = dst.get_furn_t();
// Allow weaker fires to spread occasionally
const int power = cur.get_field_intensity() + one_in( 5 );
if( can_spread && rng( 1, 100 ) < spread_chance &&
( check_flammable( dster ) || check_flammable( dsfrn ) ) &&
( in_pit == ( dster.id.id() == t_pit ) ) &&
(
( power >= 3 && cur.get_field_age() < 0_turns && one_in( 20 ) ) ||
( power >= 2 && ( ter_furn_has_flag( dster, dsfrn, TFLAG_FLAMMABLE ) && one_in( 2 ) ) ) ||
( power >= 2 && ( ter_furn_has_flag( dster, dsfrn, TFLAG_FLAMMABLE_ASH ) && one_in( 2 ) ) ) ||
( power >= 3 && ( ter_furn_has_flag( dster, dsfrn, TFLAG_FLAMMABLE_HARD ) && one_in( 5 ) ) ) ||
nearwebfld || ( dst.get_item_count() > 0 &&
flammable_items_at( p + eight_horizontal_neighbors[i] ) &&
one_in( 5 ) )
) ) {
// Nearby open flammable ground? Set it on fire.
add_field( dst_p, fd_fire, 1, 0_turns, false );
tmpfld = dst.find_field( fd_fire );
if( tmpfld != nullptr ) {
// Make the new fire quite weak, so that it doesn't start jumping around instantly
tmpfld->set_field_age( 2_minutes );
// Consume a bit of our fuel
cur.set_field_age( cur.get_field_age() + 1_minutes );
}
if( nearwebfld ) {
nearwebfld->set_field_intensity( 0 );
}
}
}
}
}
// Spread gaseous fields
if( cur.gas_can_spread() ) {
const int gas_percent_spread = cur_fd_type.percent_spread;
if( gas_percent_spread > 0 ) {
const time_duration outdoor_age_speedup = cur_fd_type.outdoor_age_speedup;
spread_gas( cur, p, gas_percent_spread, outdoor_age_speedup, sblk );
}
}
if( cur_fd_type_id == fd_fungal_haze ) {
if( one_in( 10 - 2 * cur.get_field_intensity() ) ) {
// Haze'd terrain
fungal_effects( *g, here ).spread_fungus( p );
}
}
// Process npc complaints
const std::tuple<int, std::string, time_duration, std::string> &npc_complain_data =
cur_fd_type.npc_complain_data;
const int chance = std::get<0>( npc_complain_data );
if( chance > 0 && one_in( chance ) ) {
if( npc *const np = g->critter_at<npc>( p, false ) ) {
np->complain_about( std::get<1>( npc_complain_data ),
std::get<2>( npc_complain_data ),
std::get<3>( npc_complain_data ) );
}
}
// Apply radiation
if( cur.extra_radiation_max() > 0 ) {
int extra_radiation = rng( cur.extra_radiation_min(), cur.extra_radiation_max() );
adjust_radiation( p, extra_radiation );
}
// Apply wandering fields from vents
if( cur_fd_type.wandering_field ) {
for( const tripoint &pnt : points_in_radius( p, cur.get_field_intensity() - 1 ) ) {
field &wandering_field = get_field( pnt );
tmpfld = wandering_field.find_field( cur_fd_type.wandering_field );
if( tmpfld && tmpfld->get_field_intensity() < cur.get_field_intensity() ) {
tmpfld->set_field_intensity( tmpfld->get_field_intensity() + 1 );
} else {
add_field( pnt, cur_fd_type.wandering_field, cur.get_field_intensity() );
}
}
}
if( cur_fd_type_id == fd_fire_vent ) {
if( cur.get_field_intensity() > 1 ) {
if( one_in( 3 ) ) {
cur.set_field_intensity( cur.get_field_intensity() - 1 );
}
create_hot_air( p, cur.get_field_intensity() );
} else {
dirty_transparency_cache = true;
add_field( p, fd_flame_burst, 3, cur.get_field_age() );
cur.set_field_intensity( 0 );
}
}
if( cur_fd_type_id == fd_flame_burst ) {
if( cur.get_field_intensity() > 1 ) {
cur.set_field_intensity( cur.get_field_intensity() - 1 );
create_hot_air( p, cur.get_field_intensity() );
} else {
dirty_transparency_cache = true;
add_field( p, fd_fire_vent, 3, cur.get_field_age() );
cur.set_field_intensity( 0 );
}
}
if( cur_fd_type_id == fd_electricity ) {
// 4 in 5 chance to spread
if( !one_in( 5 ) ) {
std::vector<tripoint> valid;
// We're grounded
if( impassable( p ) && cur.get_field_intensity() > 1 ) {
int tries = 0;
tripoint pnt;
pnt.z = p.z;
while( tries < 10 && cur.get_field_age() < 5_minutes && cur.get_field_intensity() > 1 ) {
pnt.x = p.x + rng( -1, 1 );
pnt.y = p.y + rng( -1, 1 );
if( passable( pnt ) && !obstructed_by_vehicle_rotation( p, pnt ) ) {
add_field( pnt, fd_electricity, 1, cur.get_field_age() + 1_turns );
cur.set_field_intensity( cur.get_field_intensity() - 1 );
tries = 0;
} else {
tries++;
}
}
// We're not grounded; attempt to ground
} else {
for( const tripoint &dst : points_in_radius( p, 1 ) ) {
// Grounded tiles first
if( impassable( dst ) ) {
valid.push_back( dst );
}
}
// Spread to adjacent space, then
if( valid.empty() ) {
tripoint dst( p + point( rng( -1, 1 ), rng( -1, 1 ) ) );
field_entry *elec = get_field( dst ).find_field( fd_electricity );
bool pass = passable( dst ) && !obstructed_by_vehicle_rotation( p, dst );
if( pass && elec != nullptr &&
elec->get_field_intensity() < 3 ) {
elec->set_field_intensity( elec->get_field_intensity() + 1 );
cur.set_field_intensity( cur.get_field_intensity() - 1 );
} else if( pass ) {
add_field( dst, fd_electricity, 1, cur.get_field_age() + 1_turns );
}
cur.set_field_intensity( cur.get_field_intensity() - 1 );
}
while( !valid.empty() && cur.get_field_intensity() > 1 ) {
const tripoint target = random_entry_removed( valid );
add_field( target, fd_electricity, 1, cur.get_field_age() + 1_turns );
cur.set_field_intensity( cur.get_field_intensity() - 1 );
}
}
}
}
int monster_spawn_chance = cur.monster_spawn_chance();
int monster_spawn_count = cur.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-- ) {
MonsterGroupResult spawn_details = MonsterGroupManager::GetResultFromGroup(
cur.monster_spawn_group(), &monster_spawn_count );
if( !spawn_details.name ) {
continue;
}
if( const std::optional<tripoint> spawn_point = random_point(
points_in_radius( p, cur.monster_spawn_radius() ),
[this]( const tripoint & n ) {
return passable( n );
} ) ) {
add_spawn( spawn_details.name, spawn_details.pack_size, *spawn_point );
}
}
}
if( cur_fd_type_id == fd_push_items ) {
map_stack items = i_at( p );
for( auto pushee = items.begin(); pushee != items.end(); ) {
if( pushee->typeId() != itype_rock ||
pushee->age() < 1_turns ) {
pushee++;
} else {
item tmp = *pushee;
tmp.set_age( 0_turns );
pushee = items.erase( pushee );
std::vector<tripoint> valid;
for( const tripoint &dst : points_in_radius( p, 1 ) ) {
if( get_field( dst, fd_push_items ) != nullptr ) {
valid.push_back( dst );