forked from CleverRaven/Cataclysm-DDA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.cpp
1508 lines (1363 loc) · 53.5 KB
/
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 "rng.h"
#include "map.h"
#include "field.h"
#include "game.h"
#define INBOUNDS(x, y) \
(x >= 0 && x < SEEX * my_MAPSIZE && y >= 0 && y < SEEY * my_MAPSIZE)
bool vector_has(std::vector <item> vec, itype_id type);
bool map::process_fields(game *g)
{
bool found_field = false;
for (int x = 0; x < my_MAPSIZE; x++) {
for (int y = 0; y < my_MAPSIZE; y++) {
if (grid[x + y * my_MAPSIZE]->field_count > 0)
found_field |= process_fields_in_submap(g, x + y * my_MAPSIZE);
}
}
return found_field;
}
/*
Function: process_fields_in_submap
Iterates over every field on every tile of the given submap indicated by NONANT parameter gridn.
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.
*/
bool map::process_fields_in_submap(game *g, int gridn)
{
// Realistically this is always true, this function only gets called if fields exist.
bool found_field = false;
// Used to hold a copy of the current field.
// Do not addField or removeField with this variable (it's just a copy afterall).
field curfield;
// A pointer to the current field effect.
// Used to modify or otherwise get information on the field effect to update.
field_entry* cur;
//Holds m.field_at(x,y).findField(fd_some_field) type returns.
// Just to avoid typing that long string for a temp value.
field_entry* tmpfld = NULL;
field_id curtype; //Holds cur->getFieldType() as thats what the old system used before rewrite.
//Loop through all tiles in this submap indicated by gridn
for (int locx = 0; locx < SEEX; locx++) {
for (int locy = 0; locy < SEEY; locy++) {
// get a copy of the field variable from the submap;
// contains all the pointers to the real field effects.
curfield = grid[gridn]->fld[locx][locy];
for(std::vector<field_entry*>::iterator field_list_it = curfield.getFieldStart();
field_list_it != curfield.getFieldEnd(); ++field_list_it){
//Iterating through all field effects in the submap's field.
cur = (*field_list_it); //dereferencing the iterator to a field_effect pointer.
if(cur == NULL) continue; //This shouldn't happen ever, but pointer safety is number one.
// This is a translation from local coordinates to submap coords.
// All submaps are in one long 1d array.
int x = locx + SEEX * (gridn % my_MAPSIZE);
int y = locy + SEEY * int(gridn / my_MAPSIZE);
curtype = cur->getFieldType();
//Setting our return value. fd_null really doesn't exist anymore, its there for legacy support.
if (!found_field && curtype != fd_null)
found_field = true;
// Again, legacy support in the event someone Mods setFieldDensity to allow more values.
if (cur->getFieldDensity() > 3 || cur->getFieldDensity() < 1)
debugmsg("Whoooooa density of %d", cur->getFieldDensity());
// Don't process "newborn" fields. This gives the player time to run if they need to.
if (cur->getFieldAge() == 0)
curtype = fd_null;
int part;
vehicle *veh;
switch (curtype) {
case fd_null:
break; // Do nothing, obviously. OBVIOUSLY.
case fd_blood:
case fd_bile:
case fd_gibs_flesh:
case fd_gibs_veggy:
if (has_flag(swimmable, x, y)) // Dissipate faster in water
cur->setFieldAge(cur->getFieldAge() + 250);
break;
case fd_acid:
if (has_flag(swimmable, x, y)) // Dissipate faster in water
cur->setFieldAge(cur->getFieldAge() + 20);
for (int i = 0; i < i_at(x, y).size(); i++) {
item *melting = &(i_at(x, y)[i]); //For each item on the tile...
// see DEVELOPER_FAQ.txt for how acid resistance is calculated
int chance = melting->acid_resist();
if (chance == 0)
{
melting->damage++;
}
else if (chance > 0 && chance < 9)
{
if (one_in(chance))
{
melting->damage++;
}
}
if (melting->damage >= 5)
{
//Destroy the object, age the field.
cur->setFieldAge(cur->getFieldAge() + melting->volume());
for (int m = 0; m < i_at(x, y)[i].contents.size(); m++)
i_at(x, y).push_back( i_at(x, y)[i].contents[m] );
i_at(x, y).erase(i_at(x, y).begin() + i);
i--;
}
}
break;
case fd_sap:
break;
case fd_sludge:
break;
// TODO-MATERIALS: use fire resistance
case fd_fire: {
// Consume items as fuel to help us grow/last longer.
bool destroyed = false; //Is the item destroyed?
// Volume, Smoke generation probability, consumed items count
int vol = 0, smoke = 0, consumed = 0;
for (int i = 0; i < i_at(x, y).size() && consumed < cur->getFieldDensity() * 2; i++) {
//Stop when we hit the end of the item buffer OR we consumed enough items given our fire size.
destroyed = false;
vol = i_at(x, y)[i].volume(); //Used to feed the fire based on volume of item burnt.
item *it = &(i_at(x, y)[i]); //Pointer to the item we are dealing with.
it_ammo *ammo_type = NULL; //Special case if its ammo.
if (it->is_ammo())
{
ammo_type = dynamic_cast<it_ammo*>(it->type);
}
//Flame type ammo removed so gasoline isn't explosive, it just burns.
if(ammo_type != NULL &&
(ammo_type->ammo_effects.count("INCENDIARY") ||
ammo_type->ammo_effects.count("EXPLOSIVE") ||
ammo_type->ammo_effects.count("FRAG") ||
ammo_type->ammo_effects.count("NAPALM") ||
ammo_type->ammo_effects.count("EXPLOSIVE_BIG") ||
ammo_type->ammo_effects.count("TEARGAS") ||
ammo_type->ammo_effects.count("SMOKE") ||
ammo_type->ammo_effects.count("FLASHBANG") ||
ammo_type->ammo_effects.count("COOKOFF")))
{
//Any kind of explosive ammo (IE: not arrows and pebbles and such)
const int rounds_exploded = rng(1, it->charges);
// TODO: Vary the effect based on the ammo flag instead of just exploding them all.
// cook off ammo instead of just burning it.
for(int j = 0; j < (rounds_exploded / 10) + 1; j++)
{
//Blow up with half the ammos damage in force, for each bullet.
g->explosion(x, y, ammo_type->damage / 2, true, false);
}
it->charges -= rounds_exploded; //Get rid of the spent ammo.
if(it->charges == 0) destroyed = true; //No more ammo, item should be removed.
} else if (it->made_of("paper")) {
//paper items feed the fire moderatly.
destroyed = it->burn(cur->getFieldDensity() * 3);
consumed++;
if (cur->getFieldDensity() == 1)
cur->setFieldAge(cur->getFieldAge() - vol * 10); //lower age is a longer lasting fire
if (vol >= 4)
smoke++; //Large paper items give chance to smoke.
} else if ((it->made_of("wood") || it->made_of("veggy"))) {
//Wood or vegy items burn slowly.
if (vol <= cur->getFieldDensity() * 10 || cur->getFieldDensity() == 3) {
cur->setFieldAge(cur->getFieldAge() - 20);
destroyed = it->burn(cur->getFieldDensity());
smoke++;
consumed++;
} else if (it->burnt < cur->getFieldDensity()) {
destroyed = it->burn(1);
smoke++;
}
} else if ((it->made_of("cotton") || it->made_of("wool"))) {
//Cotton and Wool burn slowly but don't feed the fire much.
if (vol <= cur->getFieldDensity() * 5 || cur->getFieldDensity() == 3) {
cur->setFieldAge(cur->getFieldAge() - 1);
destroyed = it->burn(cur->getFieldDensity());
smoke++;
consumed++;
} else if (it->burnt < cur->getFieldDensity()) {
destroyed = it->burn(1);
smoke++;
}
} else if ((it->made_of("flesh"))||(it->made_of("hflesh"))) {
//Same as cotton/wool really but more smokey.
if (vol <= cur->getFieldDensity() * 5 || (cur->getFieldDensity() == 3 && one_in(vol / 20))) {
cur->setFieldAge(cur->getFieldAge() - 1);
destroyed = it->burn(cur->getFieldDensity());
smoke += 3;
consumed++;
} else if (it->burnt < cur->getFieldDensity() * 5 || cur->getFieldDensity() >= 2) {
destroyed = it->burn(1);
smoke++;
}
} else if (it->made_of(LIQUID)) {
//Lots of smoke if alcohol, and LOTS of fire fueling power, kills a fire otherwise.
if(it->type->id == "tequila" || it->type->id == "whiskey" ||
it->type->id == "vodka" || it->type->id == "rum" || it->type->id == "gasoline") {
cur->setFieldAge(cur->getFieldAge() - 300);
smoke += 6;
} else {
cur->setFieldAge(cur->getFieldAge() + rng(80 * vol, 300 * vol));
smoke++;
}
it->charges -= cur->getFieldDensity();
if(it->charges <= 0){
destroyed = true;
}
consumed++;
} else if (it->made_of("powder")) {
//Any powder will fuel the fire as much as its volume but be immediately destroyed.
cur->setFieldAge(cur->getFieldAge() - vol);
destroyed = true;
smoke += 2;
} else if (it->made_of("plastic")) {
//Smokey material, doesn't fuel well.
smoke += 3;
if (it->burnt <= cur->getFieldDensity() * 2 || (cur->getFieldDensity() == 3 && one_in(vol))) {
destroyed = it->burn(cur->getFieldDensity());
if (one_in(vol + it->burnt))
cur->setFieldAge(cur->getFieldAge() - 1);
}
}
if (destroyed) {
//If we decided the item was destroyed by fire, remove it.
for (int m = 0; m < i_at(x, y)[i].contents.size(); m++)
i_at(x, y).push_back( i_at(x, y)[i].contents[m] );
i_at(x, y).erase(i_at(x, y).begin() + i);
i--;
}
}
veh = veh_at(x, y, part); //Get the part of the vehicle in the fire.
if (veh)
veh->damage (part, cur->getFieldDensity() * 10, false); //Damage the vehicle in the fire.
// If the flames are in a brazier, they're fully contained, so skip consuming terrain
if((tr_brazier != tr_at(x, y))&&(has_flag(fire_container, x, y) != true )) {
// Consume the terrain we're on
if (has_flag(explodes, x, y)) {
//This is what destroys houses so fast.
ter_set(x, y, ter_id(int(ter(x, y)) + 1));
cur->setFieldAge(0); //Fresh level 3 fire.
cur->setFieldDensity(3);
g->explosion(x, y, 40, 0, true); //Boom.
} else if (has_flag(flammable, x, y) && one_in(32 - cur->getFieldDensity() * 10)) {
//The fire feeds on the ground itself until max density.
cur->setFieldAge(cur->getFieldAge() - cur->getFieldDensity() * cur->getFieldDensity() * 40);
smoke += 15;
if (cur->getFieldDensity() == 3)
g->m.destroy(g, x, y, false);
} else if (has_flag(flammable2, x, y) && one_in(32 - cur->getFieldDensity() * 10)) {
//The fire feeds on the ground itself until max density.
cur->setFieldAge(cur->getFieldAge() - cur->getFieldDensity() * cur->getFieldDensity() * 40);
smoke += 15;
if (cur->getFieldDensity() == 3){
ter_set(x, y, t_ash);
if(has_furn(x,y))
furn_set(x,y,f_null);
}
} else if (has_flag(l_flammable, x, y) && one_in(62 - cur->getFieldDensity() * 10)) {
//The fire feeds on the ground itself until max density.
cur->setFieldAge(cur->getFieldAge() - cur->getFieldDensity() * cur->getFieldDensity() * 30);
smoke += 10;
if (cur->getFieldDensity() == 3)
g->m.destroy(g, x, y, false);
} else if (terlist[ter(x, y)].flags & mfb(swimmable))
cur->setFieldAge(cur->getFieldAge() + 800); // Flames die quickly on water
}
// If we consumed a lot, the flames grow higher
while (cur->getFieldDensity() < 3 && cur->getFieldAge() < 0) {
//Fires under 0 age grow in size. Level 3 fires under 0 spread later on.
cur->setFieldAge(cur->getFieldAge() + 300);
cur->setFieldDensity(cur->getFieldDensity() + 1);
}
// If the flames are in a pit, it can't spread to non-pit
bool in_pit = (ter(x, y) == t_pit);
// If the flames are REALLY big, they contribute to adjacent flames
if (cur->getFieldDensity() == 3 && cur->getFieldAge() < 0 && tr_brazier != tr_at(x, y)
&& (has_flag(fire_container, x, y) != true ) ){
// Randomly offset our x/y shifts by 0-2, to randomly pick a square to spread to
int starti = rng(0, 2);
int startj = rng(0, 2);
tmpfld = NULL;
// Basically: Scan around for a spot,
// if there is more fire there, make it bigger and both flames renew in power
// This is how level 3 fires spend their excess age: making other fires bigger. Flashpoint.
for (int i = 0; i < 3 && cur->getFieldAge() < 0; i++) {
for (int j = 0; j < 3 && cur->getFieldAge() < 0; j++) {
int fx = x + ((i + starti) % 3) - 1, fy = y + ((j + startj) % 3) - 1;
tmpfld = field_at(fx,fy).findField(fd_fire);
if (tmpfld && tmpfld != cur && cur->getFieldAge() < 0 && tmpfld->getFieldDensity() < 3 &&
(in_pit == (ter(fx, fy) == t_pit))) {
tmpfld->setFieldDensity(tmpfld->getFieldDensity() + 1);
//tmpfld->setFieldAge(0);
cur->setFieldAge(cur->getFieldAge() + 150);
}
}
}
}
// Consume adjacent fuel / terrain / webs to spread.
// Randomly offset our x/y shifts by 0-2, to randomly pick a square to spread to
//Fires can only spread under 30 age. This is arbitrary but seems to work well.
//The reason is to differentiate a fire that spawned vs one created really.
//Fires spawned by fire in a new square START at 30 age, so if its a square with no fuel on it
//the fire won't keep crawling endlessly across the map.
int starti = rng(0, 2);
int startj = rng(0, 2);
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
int fx = x + ((i + starti) % 3) - 1, fy = y + ((j + startj) % 3) - 1;
if (INBOUNDS(fx, fy)) {
int spread_chance = 25 * (cur->getFieldDensity() - 1);
if (field_at(fx, fy).findField(fd_web))
spread_chance = 50 + spread_chance / 2;
if (has_flag(explodes, fx, fy) && one_in(8 - cur->getFieldDensity()) &&
tr_brazier != tr_at(x, y) && (has_flag(fire_container, x, y) != true ) ) {
ter_set(fx, fy, ter_id(int(ter(fx, fy)) + 1));
g->explosion(fx, fy, 40, 0, true); //Nearby explodables? blow em up.
} else if ((i != 0 || j != 0) && rng(1, 100) < spread_chance && cur->getFieldAge() < 200 &&
tr_brazier != tr_at(x, y) &&
(has_flag(fire_container, x, y) != true )&&
(in_pit == (ter(fx, fy) == t_pit)) &&
(
(cur->getFieldDensity() >= 2 &&
(has_flag(flammable, fx, fy) && one_in(20))) ||
(cur->getFieldDensity() >= 2 &&
(has_flag(flammable2, fx, fy) && one_in(10))) ||
(cur->getFieldDensity() == 3 &&
(has_flag(l_flammable, fx, fy) && one_in(10))) ||
flammable_items_at(fx, fy) ||
field_at(fx, fy).findField(fd_web))) {
add_field(g, fx, fy, fd_fire, 1); //Nearby open flammable ground? Set it on fire.
tmpfld = field_at(fx,fy).findField(fd_fire);
if(tmpfld){
tmpfld->setFieldAge(100);
cur->setFieldAge(cur->getFieldAge() + 50);
}
if(field_at(fx,fy).findField(fd_web))
g->m.remove_field(fx,fy,fd_web);
} else {
bool nosmoke = true;
for (int ii = -1; ii <= 1; ii++) {
for (int jj = -1; jj <= 1; jj++) {
if (field_at(x+ii, y+jj).findField(fd_fire) &&
field_at(x+ii, y+jj).findField(fd_fire)->getFieldDensity() == 3)
smoke++; //The higher this gets, the more likely for smoke.
else if (field_at(x+ii, y+jj).findField(fd_fire) &&
field_at(x+ii, y+jj).findField(fd_fire)->getFieldDensity() == 2 && one_in(4))
smoke++;
else if (field_at(x+ii, y+jj).findField(fd_smoke))
nosmoke = false; //slightly, slightly, less likely to make smoke if there is already smoke
}
}
// If we're not spreading, maybe we'll stick out some smoke, huh?
if(!(is_outside(fx, fy))){
//Lets make more smoke indoors since it doesn't dissipate
smoke += 5; //10 is just a magic number. To much smoke indoors? Lower it. To little, raise it.
}
if (move_cost(fx, fy) > 0 &&
(!one_in(smoke) || (nosmoke && one_in(40))) &&
rng(3, 35) < cur->getFieldDensity() * 5 && cur->getFieldAge() < 1000 &&
(has_flag(suppress_smoke, x, y) != true )) {
smoke--;
add_field(g, fx, fy, fd_smoke, rng(1, cur->getFieldDensity())); //Add smoke!
}
}
}
}
}
} break;
case fd_smoke:
//Smoke likes to spread out, and lingers indoors.
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++)
g->scent(x+i, y+j) = 0; //Smoke removes scent from any adjacent square.
}
if (is_outside(x, y))
cur->setFieldAge(cur->getFieldAge() + 50); //Dissipiate faster outdoors.
if (!one_in(5)) { //80% chance to spread around.
std::vector <point> spread;
for (int a = -1; a <= 1; a++) {
for (int b = -1; b <= 1; b++) {
if ((field_at(x+a, y+b).findField(fd_smoke) &&
field_at(x+a, y+b).findField(fd_smoke)->getFieldDensity() < 3) ||
(move_cost(x+a, y+b) > 0))
spread.push_back(point(x+a, y+b)); //Locating all available spots.
}
}
// Spread if available and large.
while(spread.size() > 0 && cur->isAlive()){
if (cur->getFieldAge() >= 0 && spread.size() > 0) {
int random_point = rng(0, spread.size() - 1);
point p = spread[random_point];
field_entry *candidate_field = field_at(p.x, p.y).findField(fd_smoke);
if (candidate_field && candidate_field->getFieldDensity() < 3) {
candidate_field->setFieldDensity(candidate_field->getFieldDensity() + 1);
cur->setFieldDensity(cur->getFieldDensity() - 1);
} else if (move_cost(p.x, p.y) > 0 &&
add_field(g, p.x, p.y, fd_smoke, 1)){
cur->setFieldDensity(cur->getFieldDensity() - 1);
if(field_at(p.x,p.y).findField(fd_smoke))
field_at(p.x, p.y).findField(fd_smoke)->setFieldAge(cur->getFieldAge());
}
spread.erase(spread.begin() + random_point);
}
}
}
break;
case fd_tear_gas:
// Reset nearby scents to zero
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++)
g->scent(x+i, y+j) = 0;
}
if (is_outside(x, y))
cur->setFieldAge(cur->getFieldAge() + 30);
// One in three chance that it spreads (less than smoke!)
if (one_in(3)) {
std::vector <point> spread;
// Pick all eligible points to spread to
for (int a = -1; a <= 1; a++) {
for (int b = -1; b <= 1; b++) {
if (((field_at(x+a, y+b).findField(fd_tear_gas)) &&
field_at(x+a, y+b).findField(fd_tear_gas)->getFieldDensity() < 3) ||
(move_cost(x+a, y+b) > 0)) //obviously can never be false, this is just to avoid stupid errors deleting code.
spread.push_back(point(x+a, y+b));
}
}
// Then, spread to a nearby point
if (cur->getFieldDensity() > 1 && cur->getFieldAge() > 0 && spread.size() > 0) {
point p = spread[rng(0, spread.size() - 1)];
// Nearby teargas grows thicker
field_entry *candidate_field = field_at(p.x, p.y).findField(fd_tear_gas);
if ( candidate_field && candidate_field->getFieldDensity() < 3) {
candidate_field->setFieldDensity(candidate_field->getFieldDensity() + 1);
cur->setFieldDensity(cur->getFieldDensity() - 1);
// Nearby smoke is converted into teargas
// Or, just create a new field.
} else if (cur->getFieldDensity() > 1 && move_cost(p.x, p.y) > 0 &&
add_field(g, p.x, p.y, fd_tear_gas, 1)) {
cur->setFieldDensity(cur->getFieldDensity() - 1);
if(field_at(p.x,p.y).findField(fd_tear_gas))
field_at(p.x, p.y).findField(fd_tear_gas)->setFieldAge(cur->getFieldAge());
}
}
}
break;
case fd_toxic_gas:
// Reset nearby scents to zero
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++)
g->scent(x+i, y+j) = 0;
}
if (is_outside(x, y))
cur->setFieldAge(cur->getFieldAge() + 40);
if (one_in(2)) {
std::vector <point> spread;
// Pick all eligible points to spread to
for (int a = -1; a <= 1; a++) {
for (int b = -1; b <= 1; b++) {
if ((field_at(x+a, y+b).findField(fd_toxic_gas) &&
field_at(x+a, y+b).findField(fd_toxic_gas)->getFieldDensity() < 3) ||
(move_cost(x+a, y+b) > 0))
spread.push_back(point(x+a, y+b));
}
}
// Then, spread to a nearby point
if (cur->getFieldDensity() > 1 && cur->getFieldAge() > 0 && spread.size() > 0) {
point p = spread[rng(0, spread.size() - 1)];
// Nearby toxic gas grows thicker
if (field_at(p.x, p.y).findField(fd_toxic_gas) &&
field_at(p.x, p.y).findField(fd_toxic_gas)->getFieldDensity() < 3) {
field_at(p.x, p.y).findField(fd_toxic_gas)->setFieldDensity(field_at(p.x, p.y).findField(fd_toxic_gas)->getFieldDensity() + 1);
cur->setFieldDensity(cur->getFieldDensity() - 1);
// Or, just create a new field.
} else if (cur->getFieldDensity() > 1 && move_cost(p.x, p.y) > 0 &&
add_field(g, p.x, p.y, fd_toxic_gas, 1)) {
cur->setFieldDensity(cur->getFieldDensity() - 1);
if(field_at(p.x,p.y).findField(fd_toxic_gas)) field_at(p.x, p.y).findField(fd_toxic_gas)->setFieldAge(cur->getFieldAge());
}
}
}
break;
case fd_nuke_gas:
// Reset nearby scents to zero
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++)
g->scent(x+i, y+j) = 0;
}
if (is_outside(x, y))
cur->setFieldAge(cur->getFieldAge() + 10);
// Increase long-term radiation in the land underneath
radiation(x, y) += rng(0, cur->getFieldDensity());
if (one_in(2)) {
std::vector <point> spread;
// Pick all eligible points to spread to
for (int a = -1; a <= 1; a++) {
for (int b = -1; b <= 1; b++) {
if (((field_at(x+a, y+b).findField(fd_nuke_gas)) &&
field_at(x+a, y+b).findField(fd_nuke_gas)->getFieldDensity() < 3 ) ||
(move_cost(x+a, y+b) > 0))
spread.push_back(point(x+a, y+b));
}
}
// Then, spread to a nearby point
if (cur->getFieldDensity() > 1 && cur->getFieldAge() > 0 && spread.size() > 0) {
point p = spread[rng(0, spread.size() - 1)];
// Nearby nukegas grows thicker
if (field_at(p.x, p.y).findField(fd_nuke_gas) &&
field_at(p.x, p.y).findField(fd_nuke_gas)->getFieldDensity() < 3) {
field_at(p.x, p.y).findField(fd_nuke_gas)->setFieldDensity(field_at(p.x, p.y).findField(fd_nuke_gas)->getFieldDensity() + 1);
if(cur->getFieldDensity() > 1){
cur->setFieldDensity(cur->getFieldDensity() - 1);
} else {
remove_field(p.x, p.y, fd_nuke_gas);
}
// Or, just create a new field.
} else if (cur->getFieldDensity() > 1 && move_cost(p.x, p.y) > 0 &&
add_field(g, p.x, p.y, fd_nuke_gas, 1)) {
if(cur->getFieldDensity() > 1){
cur->setFieldDensity(cur->getFieldDensity() - 1);
} else {
remove_field(p.x, p.y, fd_nuke_gas);
}
if(field_at(p.x,p.y).findField(fd_nuke_gas)) field_at(p.x, p.y).findField(fd_nuke_gas)->setFieldAge(cur->getFieldAge());
}
}
}
break;
case fd_gas_vent:
for (int i = x - 1; i <= x + 1; i++) {
for (int j = y - 1; j <= y + 1; j++) {
if (field_at(i, j).findField(fd_toxic_gas) && field_at(i, j).findField(fd_toxic_gas)->getFieldDensity() < 3)
field_at(i, j).findField(fd_toxic_gas)->setFieldDensity(field_at(i,j).findField(fd_toxic_gas)->getFieldDensity() + 1);
else
add_field(g, i, j, fd_toxic_gas, 3);
}
}
break;
case fd_fire_vent:
if (cur->getFieldDensity() > 1) {
if (one_in(3))
cur->setFieldDensity(cur->getFieldDensity() - 1);
} else {
cur->setFieldType(fd_flame_burst);
cur->setFieldDensity(3);
}
break;
case fd_flame_burst:
if (cur->getFieldDensity() > 1)
cur->setFieldDensity(cur->getFieldDensity() - 1);
else {
cur->setFieldType(fd_fire_vent);
cur->setFieldDensity(3);
}
break;
case fd_electricity:
if (!one_in(5)) { // 4 in 5 chance to spread
std::vector<point> valid;
if (move_cost(x, y) == 0 && cur->getFieldDensity() > 1) { // We're grounded
int tries = 0;
while (tries < 10 && cur->getFieldAge() < 50 && cur->getFieldDensity() > 1) {
int cx = x + rng(-1, 1), cy = y + rng(-1, 1);
if (move_cost(cx, cy) != 0) {
add_field(g, cx, cy, fd_electricity, 1);
tmpfld = field_at(cx, cy).findField(fd_electricity);
if(tmpfld) tmpfld->setFieldAge(cur->getFieldAge() + 1);
cur->setFieldDensity(cur->getFieldDensity() - 1);
tries = 0;
} else
tries++;
}
} else { // We're not grounded; attempt to ground
for (int a = -1; a <= 1; a++) {
for (int b = -1; b <= 1; b++) {
if (move_cost(x + a, y + b) == 0) // Grounded tiles first
valid.push_back(point(x + a, y + b));
}
}
if (valid.size() == 0) { // Spread to adjacent space, then
int px = x + rng(-1, 1), py = y + rng(-1, 1);
if (move_cost(px, py) > 0 && field_at(px, py).findField(fd_electricity) &&
field_at(px, py).findField(fd_electricity)->getFieldDensity() < 3){
field_at(px, py).findField(fd_electricity)->setFieldDensity(field_at(px,py).findField(fd_electricity)->getFieldDensity() + 1);
cur->setFieldDensity(cur->getFieldDensity() - 1);
}
else if (move_cost(px, py) > 0){
add_field(g, px, py, fd_electricity, 1);
tmpfld = field_at(px, py).findField(fd_electricity);
if(tmpfld) tmpfld->setFieldAge(cur->getFieldAge() + 1);
}
cur->setFieldDensity(cur->getFieldDensity() - 1);
}
while (valid.size() > 0 && cur->getFieldDensity() > 1) {
int index = rng(0, valid.size() - 1);
add_field(g, valid[index].x, valid[index].y, fd_electricity, 1);
tmpfld = field_at(valid[index].x, valid[index].y).findField(fd_electricity);
if(tmpfld) tmpfld->setFieldAge(cur->getFieldAge() + 1);
cur->setFieldDensity(cur->getFieldDensity() - 1);
valid.erase(valid.begin() + index);
}
}
}
break;
case fd_fatigue:
if (cur->getFieldDensity() < 3 && int(g->turn) % 3600 == 0 && one_in(10))
cur->setFieldDensity(cur->getFieldDensity() + 1);
else if (cur->getFieldDensity() == 3 && one_in(600)) { // Spawn nether creature!
mon_id type = mon_id(rng(mon_flying_polyp, mon_blank));
monster creature(g->mtypes[type]);
creature.spawn(x + rng(-3, 3), y + rng(-3, 3));
g->z.push_back(creature);
}
break;
case fd_push_items: {
std::vector<item> *it = &(i_at(x, y));
for (int i = 0; i < it->size(); i++) {
if ((*it)[i].type->id != "rock" || (*it)[i].bday >= int(g->turn) - 1)
i++;
else {
item tmp = (*it)[i];
tmp.bday = int(g->turn);
it->erase(it->begin() + i);
i--;
std::vector<point> valid;
for (int xx = x - 1; xx <= x + 1; xx++) {
for (int yy = y - 1; yy <= y + 1; yy++) {
if (field_at(xx, yy).findField(fd_push_items))
valid.push_back( point(xx, yy) );
}
}
if (!valid.empty()) {
point newp = valid[rng(0, valid.size() - 1)];
add_item(newp.x, newp.y, tmp);
if (g->u.posx == newp.x && g->u.posy == newp.y) {
g->add_msg("A %s hits you!", tmp.tname().c_str());
g->u.hit(g, random_body_part(), rng(0, 1), 6, 0);
}
int npcdex = g->npc_at(newp.x, newp.y),
mondex = g->mon_at(newp.x, newp.y);
if (npcdex != -1) {
npc *p = g->active_npc[npcdex];
p->hit(g, random_body_part(), rng(0, 1), 6, 0);
if (g->u_see(newp.x, newp.y))
g->add_msg("A %s hits %s!", tmp.tname().c_str(), p->name.c_str());
}
if (mondex != -1) {
monster *mon = &(g->z[mondex]);
mon->hurt(6 - mon->armor_bash());
if (g->u_see(newp.x, newp.y))
g->add_msg("A %s hits the %s!", tmp.tname().c_str(),
mon->name().c_str());
}
}
}
}
} break;
case fd_shock_vent:
if (cur->getFieldDensity() > 1) {
if (one_in(5))
cur->setFieldDensity(cur->getFieldDensity() - 1);
} else {
cur->setFieldDensity(3);
int num_bolts = rng(3, 6);
for (int i = 0; i < num_bolts; i++) {
int xdir = 0, ydir = 0;
while (xdir == 0 && ydir == 0) {
xdir = rng(-1, 1);
ydir = rng(-1, 1);
}
int dist = rng(4, 12);
int boltx = x, bolty = y;
for (int n = 0; n < dist; n++) {
boltx += xdir;
bolty += ydir;
add_field(g, boltx, bolty, fd_electricity, rng(2, 3));
if (one_in(4)) {
if (xdir == 0)
xdir = rng(0, 1) * 2 - 1;
else
xdir = 0;
}
if (one_in(4)) {
if (ydir == 0)
ydir = rng(0, 1) * 2 - 1;
else
ydir = 0;
}
}
}
}
break;
case fd_acid_vent:
if (cur->getFieldDensity() > 1) {
if (cur->getFieldAge() >= 10) {
cur->setFieldDensity(cur->getFieldDensity() - 1);
cur->setFieldAge(0);
}
} else {
cur->setFieldDensity(3);
for (int i = x - 5; i <= x + 5; i++) {
for (int j = y - 5; j <= y + 5; j++) {
if (field_at(i, j).findField(fd_acid) || field_at(i, j).findField(fd_acid)->getFieldDensity() == 0) {
int newdens = 3 - (rl_dist(x, y, i, j) / 2) + (one_in(3) ? 1 : 0);
if (newdens > 3)
newdens = 3;
if (newdens > 0)
add_field(g, i, j, fd_acid, newdens);
}
}
}
}
break;
} // switch (curtype)
bool should_dissipate = false;
cur->setFieldAge(cur->getFieldAge() + 1);
if (fieldlist[cur->getFieldType()].halflife > 0) {
if (cur->getFieldAge() > 0 &&
dice(2, cur->getFieldAge()) > fieldlist[cur->getFieldType()].halflife) {
cur->setFieldAge(0);
if(cur->getFieldDensity() == 1 || !cur->isAlive()){
should_dissipate = true;
}
cur->setFieldDensity(cur->getFieldDensity() - 1);
}
if (should_dissipate == true || !cur->isAlive()) { // Totally dissapated.
grid[gridn]->field_count--;
grid[gridn]->fld[locx][locy].removeField(cur->getFieldType());
}
}
}
}
}
return found_field;
}
//This entire function makes very little sense. Why are the rules the way they are? Why does walking into some things destroy them but not others?
/*
Function: step_in_field
Triggers any active abilities a field effect would have. Fire burns you, acid melts you, etc.
If you add a field effect that interacts with the player place a case statement in the switch here.
If you wish for a field effect to do something over time (propagate, interact with terrain, etc) place it in process_subfields
*/
void map::step_in_field(int x, int y, game *g)
{
field curfield = field_at(x, y); //A copy of the current field for reference. Do not add fields to it, use map::add_field
field_entry *cur = NULL; //The current field effect.
int veh_part; //vehicle part existing on this tile.
vehicle *veh = NULL; //Vehicle reference if there is one.
bool inside = false; //Are we inside?
bool no_rubble = true; //For use in determining if we are in a rubble square or not, for the disease effect
int adjusted_intensity; //to modify power of a field based on... whatever is relevant for the effect.
//If we are in a vehicle figure out if we are inside (reduces effects usually) and what part of the vehicle we need to deal with.
if (g->u.in_vehicle) {
veh = g->m.veh_at(x, y, veh_part);
inside = (veh && veh->is_inside(veh_part));
}
//Iterate through all field effects on this tile.
for(std::vector<field_entry*>::iterator field_list_it = curfield.getFieldStart(); field_list_it != curfield.getFieldEnd(); ++field_list_it){
cur = (*field_list_it);
if(cur == NULL) continue; //shouldn't happen unless you free memory of field entries manually (hint: don't do that)... Pointer safety.
if (cur->getFieldType() == fd_rubble){
no_rubble = false; //We found rubble, don't remove the players rubble disease at the end of function.
}
//Do things based on what field effect we are currently in.
switch (cur->getFieldType()) {
case fd_null:
case fd_blood: // It doesn't actually do anything
case fd_bile: // Ditto
break; //break instead of return in the event of post-processing in the future; also we're in a loop now!
case fd_web: {
//If we are in a web, can't walk in webs or are in a vehicle, get webbed maybe.
//Moving through multiple webs stacks the effect.
if (!g->u.has_trait(PF_WEB_WALKER) && !g->u.in_vehicle) {
int web = cur->getFieldDensity() * 5 - g->u.disease_level("webbed"); //between 5 and 15 minus your current web level.
if (web > 0)
g->u.add_disease("webbed", web);
remove_field(x, y, fd_web); //Its spent.
} else if (g->u.in_vehicle){ //If you are in a vehicle destroy the web. It should of been destroyed when you ran over it anyway.
remove_field(x, y, fd_web);
}
} break;
case fd_acid:
//Acid deals damage at all levels now; the inside refers to inside a vehicle.
//TODO: Add resistance to this with rubber shoes or something?
if (cur->getFieldDensity() == 3 && !inside) {
g->add_msg("The acid burns your legs and feet!");
g->u.hit(g, bp_feet, 0, 0, rng(4, 10));
g->u.hit(g, bp_feet, 1, 0, rng(4, 10));
g->u.hit(g, bp_legs, 0, 0, rng(2, 8));
g->u.hit(g, bp_legs, 1, 0, rng(2, 8));
} else if (cur->getFieldDensity() == 2 && !inside) {
g->u.hit(g, bp_feet, 0, 0, rng(2, 5));
g->u.hit(g, bp_feet, 1, 0, rng(2, 5));
g->u.hit(g, bp_legs, 0, 0, rng(1, 4));
g->u.hit(g, bp_legs, 1, 0, rng(1, 4));
} else if (!inside) {
g->u.hit(g, bp_feet, 0, 0, rng(1, 3));
g->u.hit(g, bp_feet, 1, 0, rng(1, 3));
g->u.hit(g, bp_legs, 0, 0, rng(0, 2));
g->u.hit(g, bp_legs, 1, 0, rng(0, 2));
}
break;
case fd_sap:
//Sap causes the player to get sap disease, slowing them down.
if( g->u.in_vehicle ) break; //sap does nothing to cars.
g->add_msg("The sap sticks to you!");
g->u.add_disease("sap", cur->getFieldDensity() * 2);
if (cur->getFieldDensity() == 1)
remove_field(x, y, fd_sap);
else
cur->setFieldDensity(cur->getFieldDensity() - 1); //Use up sap.
break;
case fd_sludge:
g->add_msg("The sludge is thick and sticky.");
break;
case fd_fire:
//Burn the player. Less so if you are in a car or ON a car.
adjusted_intensity = cur->getFieldDensity();
if( g->u.in_vehicle )
{
if( inside )
{
adjusted_intensity -= 2;
}
else
{
adjusted_intensity -= 1;
}
}
if (!g->u.has_active_bionic("bio_heatsink")) { //heatsink prevents ALL fire damage.
if (adjusted_intensity == 1) {
g->add_msg("You burn your legs and feet!");
g->u.hit(g, bp_feet, 0, 0, rng(2, 6));
g->u.hit(g, bp_feet, 1, 0, rng(2, 6));
g->u.hit(g, bp_legs, 0, 0, rng(1, 4));
g->u.hit(g, bp_legs, 1, 0, rng(1, 4));
} else if (adjusted_intensity == 2) {
g->add_msg("You're burning up!");
g->u.hit(g, bp_legs, 0, 0, rng(2, 6));
g->u.hit(g, bp_legs, 1, 0, rng(2, 6));
g->u.hit(g, bp_torso, 0, 4, rng(4, 9));
} else if (adjusted_intensity == 3) {
g->add_msg("You're set ablaze!");
g->u.hit(g, bp_legs, 0, 0, rng(2, 6));
g->u.hit(g, bp_legs, 1, 0, rng(2, 6));
g->u.hit(g, bp_torso, 0, 4, rng(4, 9));
g->u.add_disease("onfire", 5); //lasting fire damage only from the strongest fires.
}
/*if (adjusted_intensity == 2)
g->u.infect("smoke", bp_mouth, 5, 20, g);
else if (adjusted_intensity == 3)
g->u.infect("smoke", bp_mouth, 7, 30, g);*/ //Removed from here since smoke now exists on fire tiles as its own effect.
}
break;
case fd_rubble:
//You are walking on rubble. Slow down.
g->u.add_disease("bouldering", 0, cur->getFieldDensity(), 3);
break;
case fd_smoke:
//Get smoke disease from standing in smoke.
if (cur->getFieldDensity() == 3 && !inside)
{
g->u.infect("smoke", bp_mouth, 4, 15, g);
} else if (cur->getFieldDensity() == 2 && !inside){
g->u.infect("smoke", bp_mouth, 2, 7, g);
} else if (cur->getFieldDensity() == 1 && !inside){
g->u.infect("smoke", bp_mouth, 1, 3, g);
}
break;
case fd_tear_gas:
//Tear gas will both give you teargas disease and/or blind you.
if ((cur->getFieldDensity() > 1 || !one_in(3)) && (!inside || (inside && one_in(3))))
{
g->u.infect("teargas", bp_mouth, 5, 20, g);
}
if (cur->getFieldDensity() > 1 && (!inside || (inside && one_in(3))))
{
g->u.infect("blind", bp_eyes, cur->getFieldDensity() * 2, 10, g);
}
break;
case fd_toxic_gas:
//Toxic gas at low levels poisons you, toxic gas at high levels will cause very nasty poison.
if (cur->getFieldDensity() == 2 && (!inside || (cur->getFieldDensity() == 3 && inside)))
{
g->u.infect("poison", bp_mouth, 5, 30, g);
}
else if (cur->getFieldDensity() == 3 && !inside)
{
g->u.infect("badpoison", bp_mouth, 5, 30, g);
} else if (cur->getFieldDensity() == 1 && (!inside))
{
g->u.infect("poison", bp_mouth, 2, 10, g);
}
break;
case fd_nuke_gas:
//Get irradiated by the nuclear fallout.
g->u.radiation += rng(cur->getFieldDensity(), cur->getFieldDensity() * (cur->getFieldDensity() + 1)); //changed to min of density, not 0.
if (cur->getFieldDensity() == 3) {
g->add_msg("This radioactive gas burns!");
g->u.hurtall(rng(1, 3));
}
break;
case fd_flame_burst:
//A burst of flame? Only hits the legs and torso.
if (inside) break; //fireballs can't touch you inside a car.
if (!g->u.has_active_bionic("bio_heatsink")) { //heatsink stops fire.
g->add_msg("You're torched by flames!");
g->u.hit(g, bp_legs, 0, 0, rng(2, 6));
g->u.hit(g, bp_legs, 1, 0, rng(2, 6));
g->u.hit(g, bp_torso, 0, 4, rng(4, 9));
} else
g->add_msg("These flames do not burn you.");
break;
case fd_electricity:
if (g->u.has_artifact_with(AEP_RESIST_ELECTRICITY)) //Artifact stops electricity.
g->add_msg("The electricity flows around you.");
else {
g->add_msg("You're electrocuted!");
g->u.hurtall(rng(1, cur->getFieldDensity())); //small universal damage based on density.
if (one_in(8 - cur->getFieldDensity()) && !one_in(30 - g->u.str_cur)) { //str of 30 stops this from happening.
g->add_msg("You're paralyzed!");
g->u.moves -= rng(cur->getFieldDensity() * 150, cur->getFieldDensity() * 200); //roughly doubled duration.
}
}
break;
case fd_fatigue:
//Teleports you... somewhere.
if (rng(0, 2) < cur->getFieldDensity()) {
g->add_msg("You're violently teleported!");
g->u.hurtall(cur->getFieldDensity());
g->teleport();
}
break;
//Why do these get removed???
case fd_shock_vent:
//Stepping on a shock vent shuts it down.
remove_field(x, y, fd_shock_vent);
break;