forked from cataclysmbnteam/Cataclysm-BN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lightmap.cpp
2014 lines (1754 loc) · 90.1 KB
/
lightmap.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 "lightmap.h" // IWYU pragma: associated
#include "shadowcasting.h" // IWYU pragma: associated
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <optional>
#include <utility>
#include <vector>
#include "avatar.h"
#include "calendar.h"
#include "cata_unreachable.h"
#include "cata_utility.h"
#include "character.h"
#include "colony.h"
#include "cuboid_rectangle.h"
#include "field.h"
#include "fragment_cloud.h" // IWYU pragma: keep
#include "game.h"
#include "int_id.h"
#include "item.h"
#include "item_stack.h"
#include "line.h"
#include "map.h"
#include "map_iterator.h"
#include "mapdata.h"
#include "math_defines.h"
#include "monster.h"
#include "mtype.h"
#include "npc.h"
#include "player.h"
#include "point.h"
#include "string_formatter.h"
#include "submap.h"
#include "tileray.h"
#include "type_id.h"
#include "veh_type.h"
#include "vehicle.h"
#include "vpart_position.h"
#include "vpart_range.h"
#include "weather.h"
static const efftype_id effect_haslight( "haslight" );
static const efftype_id effect_onfire( "onfire" );
static constexpr int LIGHTMAP_CACHE_X = MAPSIZE_X;
static constexpr int LIGHTMAP_CACHE_Y = MAPSIZE_Y;
static constexpr point lightmap_boundary_min{};
static constexpr point lightmap_boundary_max( LIGHTMAP_CACHE_X, LIGHTMAP_CACHE_Y );
const half_open_rectangle<point> lightmap_boundaries(
lightmap_boundary_min, lightmap_boundary_max );
std::string four_quadrants::to_string() const
{
return string_format( "(%.2f,%.2f,%.2f,%.2f)",
( *this )[quadrant::NE], ( *this )[quadrant::SE],
( *this )[quadrant::SW], ( *this )[quadrant::NW] );
}
template<int n>
struct transparency_exp_lookup {
float values[n];
float transparency = LIGHT_TRANSPARENCY_OPEN_AIR;
void reset( float trans ) {
transparency = trans;
for( int i = 0; i < n; i++ ) {
values[i] = 1 / std::exp( trans * i );
}
}
transparency_exp_lookup( float trans ) : values() {
reset( trans );
}
};
//These are used for shadowcasting fast paths, openair is constant, weather is replaced every time the weather changes.
//They are 90 entries large to comfortably account for shadowcasting's limit of 60*sqrt(2).
const transparency_exp_lookup<90> openair_transparency_lookup( LIGHT_TRANSPARENCY_OPEN_AIR );
transparency_exp_lookup<90> weather_transparency_lookup( LIGHT_TRANSPARENCY_OPEN_AIR * 1.1 );
void map::add_light_from_items( const tripoint &p, item_stack::iterator begin,
item_stack::iterator end )
{
for( auto itm_it = begin; itm_it != end; ++itm_it ) {
float ilum = 0.0f; // brightness
units::angle iwidth = 0_degrees; // 0-360 degrees. 0 is a circular light_source
units::angle idir = 0_degrees; // otherwise, it's a light_arc pointed in this direction
if( itm_it->getlight( ilum, iwidth, idir ) ) {
if( iwidth > 0_degrees ) {
apply_light_arc( p, idir, ilum, iwidth );
} else {
add_light_source( p, ilum );
}
}
}
}
// TODO: Consider making this just clear the cache and dynamically fill it in as is_transparent() is called
bool map::build_transparency_cache( const int zlev )
{
auto &map_cache = get_cache( zlev );
auto &transparency_cache = map_cache.transparency_cache;
auto &outside_cache = map_cache.outside_cache;
if( map_cache.transparency_cache_dirty.none() ) {
return false;
}
std::set<tripoint> vehicles_processed;
// if true, all submaps are invalid (can use batch init)
bool rebuild_all = map_cache.transparency_cache_dirty.all();
if( rebuild_all ) {
// Default to just barely not transparent.
std::uninitialized_fill_n( &transparency_cache[0][0], MAPSIZE_X * MAPSIZE_Y,
static_cast<float>( LIGHT_TRANSPARENCY_OPEN_AIR ) );
}
const float sight_penalty = get_weather().weather_id->sight_penalty;
if( sight_penalty != 1.0f &&
LIGHT_TRANSPARENCY_OPEN_AIR * sight_penalty != weather_transparency_lookup.transparency ) {
weather_transparency_lookup.reset( LIGHT_TRANSPARENCY_OPEN_AIR * sight_penalty );
}
// Traverse the submaps in order
for( int smx = 0; smx < my_MAPSIZE; ++smx ) {
for( int smy = 0; smy < my_MAPSIZE; ++smy ) {
const auto cur_submap = get_submap_at_grid( {smx, smy, zlev} );
const point sm_offset = sm_to_ms_copy( point( smx, smy ) );
if( !rebuild_all && !map_cache.transparency_cache_dirty[smx * MAPSIZE + smy] ) {
continue;
}
// calculates transparency of a single tile
// x,y - coords in map local coords
auto calc_transp = [&]( point p ) {
const point sp = p - sm_offset;
float value = LIGHT_TRANSPARENCY_OPEN_AIR;
if( !( cur_submap->get_ter( sp ).obj().transparent &&
cur_submap->get_furn( sp ).obj().transparent ) ) {
return LIGHT_TRANSPARENCY_SOLID;
}
if( outside_cache[p.x][p.y] ) {
// FIXME: Places inside vehicles haven't been marked as
// inside yet so this is incorrectly penalising for
// weather in vehicles.
value *= sight_penalty;
}
for( const auto &fld : cur_submap->get_field( sp ) ) {
const field_entry &cur = fld.second;
if( cur.is_transparent() ) {
continue;
}
// Fields are either transparent or not, however we want some to be translucent
value = value * cur.translucency();
}
// TODO: [lightmap] Have glass reduce light as well
return value;
};
if( cur_submap->is_uniform ) {
float value = calc_transp( sm_offset );
// if rebuild_all==true all values were already set to LIGHT_TRANSPARENCY_OPEN_AIR
if( !rebuild_all || value != LIGHT_TRANSPARENCY_OPEN_AIR ) {
for( int sx = 0; sx < SEEX; ++sx ) {
// init all sy indices in one go
std::uninitialized_fill_n( &transparency_cache[sm_offset.x + sx][sm_offset.y], SEEY, value );
}
}
} else {
for( int sx = 0; sx < SEEX; ++sx ) {
const int x = sx + sm_offset.x;
for( int sy = 0; sy < SEEY; ++sy ) {
const int y = sy + sm_offset.y;
transparency_cache[x][y] = calc_transp( { x, y } );
//Nudge things towards fast paths
if( std::fabs( transparency_cache[x][y] - openair_transparency_lookup.transparency ) <= 0.0001 ) {
transparency_cache[x][y] = openair_transparency_lookup.transparency;
} else if( std::fabs( transparency_cache[x][y] - weather_transparency_lookup.transparency ) <=
0.0001 ) {
transparency_cache[x][y] = weather_transparency_lookup.transparency;
}
}
}
}
}
}
map_cache.transparency_cache_dirty.reset();
return true;
}
bool map::build_vision_transparency_cache( const Character &player )
{
const tripoint &p = player.pos();
bool dirty = false;
if( player.movement_mode_is( CMM_CROUCH ) ) {
const auto check_vehicle_coverage = []( const vehicle * veh, point p ) -> bool {
return veh->obstacle_at_position( p ) == -1 && ( veh->part_with_feature( p, "AISLE", true ) != -1 || veh->part_with_feature( p, "PROTRUSION", true ) != -1 );
};
const optional_vpart_position player_vp = veh_at( p );
point player_mount;
if( player_vp ) {
player_mount = player_vp->vehicle().tripoint_to_mount( p );
}
int i = 0;
for( point adjacent : eight_adjacent_offsets ) {
vision_transparency_cache[i] = VISION_ADJUST_NONE;
// If we're crouching behind an obstacle, we can't see past it.
if( coverage( adjacent + p ) >= 30 ) {
dirty = true;
vision_transparency_cache[i] = VISION_ADJUST_SOLID;
} else {
if( std::find( four_diagonal_offsets.begin(), four_diagonal_offsets.end(),
adjacent ) != four_diagonal_offsets.end() ) {
const optional_vpart_position adjacent_vp = veh_at( p + adjacent );
point adjacent_mount;
if( adjacent_vp ) {
adjacent_mount = adjacent_vp->vehicle().tripoint_to_mount( p );
}
if( ( player_vp &&
!player_vp->vehicle().check_rotated_intervening( player_mount,
player_vp->vehicle().tripoint_to_mount( p + adjacent ),
check_vehicle_coverage ) )
|| ( adjacent_vp && ( !player_vp || &( player_vp->vehicle() ) != &( adjacent_vp->vehicle() ) ) &&
!adjacent_vp->vehicle().check_rotated_intervening( adjacent_vp->vehicle().tripoint_to_mount(
p ), adjacent_vp->vehicle().tripoint_to_mount( p + adjacent ),
check_vehicle_coverage ) ) ) {
dirty = true;
vision_transparency_cache[ i ] = VISION_ADJUST_HIDDEN;
}
}
}
i++;
}
} else {
std::fill_n( &vision_transparency_cache[0], 8, VISION_ADJUST_NONE );
}
return dirty;
}
void map::apply_character_light( Character &p )
{
if( p.has_effect( effect_onfire ) ) {
apply_light_source( p.pos(), 8 );
} else if( p.has_effect( effect_haslight ) ) {
apply_light_source( p.pos(), 4 );
}
const float held_luminance = p.active_light();
if( held_luminance > LIGHT_AMBIENT_LOW ) {
apply_light_source( p.pos(), held_luminance );
}
if( held_luminance >= 4 && held_luminance > ambient_light_at( p.pos() ) - 0.5f ) {
p.add_effect( effect_haslight, 1_turns );
}
}
// This function raytraces starting at the upper limit of the simulated area descending
// toward the lower limit. Since it's sunlight, the rays are parallel.
// Each layer consults the next layer up to determine the intensity of the light that reaches it.
// Once this is complete, additional operations add more dynamic lighting.
void map::build_sunlight_cache( int pzlev )
{
const int zlev_min = zlevels ? -OVERMAP_DEPTH : pzlev;
// Start at the topmost populated zlevel to avoid unnecessary raycasting
// Plus one zlevel to prevent clipping inside structures
const int zlev_max = zlevels
? clamp( calc_max_populated_zlev() + 1,
std::min( OVERMAP_HEIGHT, pzlev + 1 ),
OVERMAP_HEIGHT )
: pzlev;
// true if all previous z-levels are fully transparent to light (no floors, transparency >= air)
bool fully_outside = true;
// true if no light reaches this level, i.e. there were no lit tiles on the above level (light level <= inside_light_level)
bool fully_inside = false;
// fully_outside and fully_inside define following states:
// initially: fully_outside=true, fully_inside=false (fast fill)
// ↓
// when first obstacles occur: fully_outside=false, fully_inside=false (slow quadrant logic)
// ↓
// when fully below ground: fully_outside=false, fully_inside=true (fast fill)
// Iterate top to bottom because sunlight cache needs to construct in that order.
for( int zlev = zlev_max; zlev >= zlev_min; zlev-- ) {
level_cache &map_cache = get_cache( zlev );
auto &lm = map_cache.lm;
// Grab illumination at ground level.
const float outside_light_level = g->natural_light_level( 0 );
// TODO: if zlev < 0 is open to sunlight, this won't calculate correct light, but neither does g->natural_light_level()
const float inside_light_level = ( zlev >= 0 && outside_light_level > LIGHT_SOURCE_BRIGHT ) ?
LIGHT_AMBIENT_DIM * 0.8 : LIGHT_AMBIENT_LOW;
// Handling when z-levels are disabled is based on whether a tile is considered "outside".
if( !zlevels ) {
const auto &outside_cache = map_cache.outside_cache;
for( int x = 0; x < MAPSIZE_X; x++ ) {
for( int y = 0; y < MAPSIZE_Y; y++ ) {
if( outside_cache[x][y] ) {
lm[x][y].fill( outside_light_level );
} else {
lm[x][y].fill( inside_light_level );
}
}
}
continue;
}
// all light was blocked before
if( fully_inside ) {
std::fill_n( &lm[0][0], MAPSIZE_X * MAPSIZE_Y, four_quadrants( inside_light_level ) );
continue;
}
// If there were no obstacles before this level, just apply weather illumination since there's no opportunity
// for light to be blocked.
if( fully_outside ) {
//fill with full light
std::fill_n( &lm[0][0], MAPSIZE_X * MAPSIZE_Y, four_quadrants( outside_light_level ) );
const auto &this_floor_cache = map_cache.floor_cache;
const auto &this_transparency_cache = map_cache.transparency_cache;
fully_inside = true; // recalculate
for( int x = 0; x < MAPSIZE_X; ++x ) {
for( int y = 0; y < MAPSIZE_Y; ++y ) {
// && semantics below is important, we want to skip the evaluation if possible, do not replace with &=
// fully_outside stays true if tile is transparent and there is no floor
fully_outside = fully_outside && this_transparency_cache[x][y] >= LIGHT_TRANSPARENCY_OPEN_AIR
&& !this_floor_cache[x][y];
// fully_inside stays true if tile is opaque OR there is floor
fully_inside = fully_inside && ( this_transparency_cache[x][y] <= LIGHT_TRANSPARENCY_SOLID ||
this_floor_cache[x][y] );
}
}
continue;
}
// Replace this with a calculated shift based on time of day and date.
// At first compress the angle such that it takes no more than one tile of shift per level.
// To exceed that, we'll have to handle casting light from the side instead of the top.
point offset;
const level_cache &prev_map_cache = get_cache_ref( zlev + 1 );
const auto &prev_lm = prev_map_cache.lm;
const auto &prev_transparency_cache = prev_map_cache.transparency_cache;
const auto &prev_floor_cache = prev_map_cache.floor_cache;
const auto &outside_cache = map_cache.outside_cache;
const float sight_penalty = get_weather().weather_id->sight_penalty;
// TODO: Replace these with a lookup inside the four_quadrants class.
constexpr std::array<point, 5> cardinals = {
{point_zero, point_north, point_west, point_east, point_south}
};
constexpr std::array<std::array<quadrant, 2>, 5> dir_quadrants = {{
{{quadrant::NE, quadrant::NW}},
{{quadrant::NE, quadrant::NW}},
{{quadrant::SW, quadrant::NW}},
{{quadrant::SE, quadrant::NE}},
{{quadrant::SE, quadrant::SW}},
}
};
fully_inside = true; // recalculate
// Fall back to minimal light level if we don't find anything.
std::fill_n( &lm[0][0], MAPSIZE_X * MAPSIZE_Y, four_quadrants( inside_light_level ) );
for( int x = 0; x < MAPSIZE_X; ++x ) {
for( int y = 0; y < MAPSIZE_Y; ++y ) {
// Check center, then four adjacent cardinals.
for( int i = 0; i < 5; ++i ) {
int prev_x = x + offset.x + cardinals[i].x;
int prev_y = y + offset.y + cardinals[i].y;
bool inbounds = prev_x >= 0 && prev_x < MAPSIZE_X &&
prev_y >= 0 && prev_y < MAPSIZE_Y;
if( !inbounds ) {
continue;
}
float prev_light_max;
float prev_transparency = prev_transparency_cache[prev_x][prev_y];
// This is pretty gross, this cancels out the per-tile transparency effect
// derived from weather.
if( outside_cache[x][y] ) {
prev_transparency /= sight_penalty;
}
if( prev_transparency > LIGHT_TRANSPARENCY_SOLID &&
!prev_floor_cache[prev_x][prev_y] &&
( prev_light_max = prev_lm[prev_x][prev_y].max() ) > 0.0 ) {
const float light_level = clamp( prev_light_max * LIGHT_TRANSPARENCY_OPEN_AIR / prev_transparency,
inside_light_level, prev_light_max );
if( i == 0 ) {
lm[x][y].fill( light_level );
fully_inside &= light_level <= inside_light_level;
break;
} else {
fully_inside &= light_level <= inside_light_level;
lm[x][y][dir_quadrants[i][0]] = light_level;
lm[x][y][dir_quadrants[i][1]] = light_level;
}
}
}
}
}
}
}
void map::generate_lightmap( const int zlev )
{
auto &map_cache = get_cache( zlev );
auto &lm = map_cache.lm;
auto &sm = map_cache.sm;
auto &outside_cache = map_cache.outside_cache;
auto &prev_floor_cache = get_cache( clamp( zlev + 1, -OVERMAP_DEPTH, OVERMAP_DEPTH ) ).floor_cache;
bool top_floor = zlev == OVERMAP_DEPTH;
std::memset( lm, 0, sizeof( lm ) );
std::memset( sm, 0, sizeof( sm ) );
/* Bulk light sources wastefully cast rays into neighbors; a burning hospital can produce
significant slowdown, so for stuff like fire and lava:
* Step 1: Store the position and luminance in buffer via add_light_source, for efficient
checking of neighbors.
* Step 2: After everything else, iterate buffer and apply_light_source only in non-redundant
directions
* Step 3: ????
* Step 4: Profit!
*/
auto &light_source_buffer = map_cache.light_source_buffer;
std::memset( light_source_buffer, 0, sizeof( light_source_buffer ) );
constexpr std::array<int, 4> dir_x = { { 0, -1, 1, 0 } }; // [0]
constexpr std::array<int, 4> dir_y = { { -1, 0, 0, 1 } }; // [1][X][2]
constexpr std::array<int, 4> dir_d = { { 90, 0, 180, 270 } }; // [3]
constexpr std::array<std::array<quadrant, 2>, 4> dir_quadrants = { {
{{ quadrant::NE, quadrant::NW }},
{{ quadrant::SW, quadrant::NW }},
{{ quadrant::SE, quadrant::NE }},
{{ quadrant::SE, quadrant::SW }},
}
};
const float natural_light = g->natural_light_level( zlev );
build_sunlight_cache( zlev );
apply_character_light( get_player_character() );
for( npc &guy : g->all_npcs() ) {
apply_character_light( guy );
}
std::vector<std::pair<tripoint, float>> lm_override;
// Traverse the submaps in order
for( int smx = 0; smx < my_MAPSIZE; ++smx ) {
for( int smy = 0; smy < my_MAPSIZE; ++smy ) {
const auto cur_submap = get_submap_at_grid( { smx, smy, zlev } );
for( int sx = 0; sx < SEEX; ++sx ) {
for( int sy = 0; sy < SEEY; ++sy ) {
const int x = sx + smx * SEEX;
const int y = sy + smy * SEEY;
const tripoint p( x, y, zlev );
// Project light into any openings into buildings.
if( !outside_cache[p.x][p.y] || ( !top_floor && prev_floor_cache[p.x][p.y] ) ) {
// Apply light sources for external/internal divide
for( int i = 0; i < 4; ++i ) {
point neighbour = p.xy() + point( dir_x[i], dir_y[i] );
if( lightmap_boundaries.contains( neighbour )
&& outside_cache[neighbour.x][neighbour.y] &&
( top_floor || !prev_floor_cache[neighbour.x][neighbour.y] )
) {
const float source_light =
std::min( natural_light, lm[neighbour.x][neighbour.y].max() );
if( light_transparency( p ) > LIGHT_TRANSPARENCY_SOLID ) {
update_light_quadrants( lm[p.x][p.y], source_light, quadrant::default_ );
apply_directional_light( p, dir_d[i], source_light );
} else {
update_light_quadrants( lm[p.x][p.y], source_light, dir_quadrants[i][0] );
update_light_quadrants( lm[p.x][p.y], source_light, dir_quadrants[i][1] );
}
}
}
}
if( cur_submap->get_lum( { sx, sy } ) && has_items( p ) ) {
auto items = i_at( p );
add_light_from_items( p, items.begin(), items.end() );
}
const ter_id terrain = cur_submap->get_ter( { sx, sy } );
if( terrain->light_emitted > 0 ) {
add_light_source( p, terrain->light_emitted );
}
const furn_id furniture = cur_submap->get_furn( {sx, sy } );
if( furniture->light_emitted > 0 ) {
add_light_source( p, furniture->light_emitted );
}
for( auto &fld : cur_submap->get_field( { sx, sy } ) ) {
const field_entry *cur = &fld.second;
const int light_emitted = cur->light_emitted();
if( light_emitted > 0 ) {
add_light_source( p, light_emitted );
}
const float light_override = cur->local_light_override();
if( light_override >= 0.0 ) {
lm_override.push_back( std::pair<tripoint, float>( p, light_override ) );
}
}
}
}
}
}
for( monster &critter : g->all_monsters() ) {
if( critter.is_hallucination() ) {
continue;
}
const tripoint &mp = critter.pos();
if( inbounds( mp ) ) {
if( critter.has_effect( effect_onfire ) ) {
apply_light_source( mp, 8 );
}
// TODO: [lightmap] Attach natural light brightness to creatures
// TODO: [lightmap] Allow creatures to have light attacks (i.e.: eyebot)
// TODO: [lightmap] Allow creatures to have facing and arc lights
if( critter.type->luminance > 0 ) {
apply_light_source( mp, critter.type->luminance );
}
}
}
// Apply any vehicle light sources
VehicleList vehs = get_vehicles();
for( auto &vv : vehs ) {
vehicle *v = vv.v;
auto lights = v->lights( true );
float veh_luminance = 0.0;
float iteration = 1.0;
for( const auto pt : lights ) {
const auto &vp = pt->info();
if( vp.has_flag( VPFLAG_CONE_LIGHT ) ||
vp.has_flag( VPFLAG_WIDE_CONE_LIGHT ) ) {
veh_luminance += vp.bonus / iteration;
iteration = iteration * 1.1;
}
}
for( const auto pt : lights ) {
const auto &vp = pt->info();
tripoint src = v->global_part_pos3( *pt );
if( !inbounds( src ) ) {
continue;
}
if( vp.has_flag( VPFLAG_CONE_LIGHT ) ) {
if( veh_luminance > lit_level::LIT ) {
add_light_source( src, M_SQRT2 ); // Add a little surrounding light
apply_light_arc( src, v->face.dir() + pt->direction, veh_luminance,
45_degrees );
}
} else if( vp.has_flag( VPFLAG_WIDE_CONE_LIGHT ) ) {
if( veh_luminance > lit_level::LIT ) {
add_light_source( src, M_SQRT2 ); // Add a little surrounding light
apply_light_arc( src, v->face.dir() + pt->direction, veh_luminance,
90_degrees );
}
} else if( vp.has_flag( VPFLAG_HALF_CIRCLE_LIGHT ) ) {
add_light_source( src, M_SQRT2 ); // Add a little surrounding light
apply_light_arc( src, v->face.dir() + pt->direction, vp.bonus, 180_degrees );
} else if( vp.has_flag( VPFLAG_CIRCLE_LIGHT ) ) {
const bool odd_turn = calendar::once_every( 2_turns );
if( ( odd_turn && vp.has_flag( VPFLAG_ODDTURN ) ) ||
( !odd_turn && vp.has_flag( VPFLAG_EVENTURN ) ) ||
( !( vp.has_flag( VPFLAG_EVENTURN ) || vp.has_flag( VPFLAG_ODDTURN ) ) ) ) {
add_light_source( src, vp.bonus );
}
} else {
add_light_source( src, vp.bonus );
}
}
for( const vpart_reference &vp : v->get_all_parts() ) {
const size_t p = vp.part_index();
const tripoint pp = vp.pos();
if( !inbounds( pp ) ) {
continue;
}
if( vp.has_feature( VPFLAG_CARGO ) && !vp.has_feature( "COVERED" ) ) {
add_light_from_items( pp, v->get_items( static_cast<int>( p ) ).begin(),
v->get_items( static_cast<int>( p ) ).end() );
}
}
}
/* Now that we have position and intensity of all bulk light sources, apply_ them
This may seem like extra work, but take a 12x12 raging inferno:
unbuffered: (12^2)*(160*4) = apply_light_ray x 92160
buffered: (12*4)*(160) = apply_light_ray x 7680
*/
const tripoint cache_start( 0, 0, zlev );
const tripoint cache_end( LIGHTMAP_CACHE_X, LIGHTMAP_CACHE_Y, zlev );
for( const tripoint &p : points_in_rectangle( cache_start, cache_end ) ) {
if( light_source_buffer[p.x][p.y] > 0.0 ) {
apply_light_source( p, light_source_buffer[p.x][p.y] );
}
}
for( const std::pair<tripoint, float> &elem : lm_override ) {
lm[elem.first.x][elem.first.y].fill( elem.second );
}
}
void map::add_light_source( const tripoint &p, float luminance )
{
auto &light_source_buffer = get_cache( p.z ).light_source_buffer;
light_source_buffer[p.x][p.y] = std::max( luminance, light_source_buffer[p.x][p.y] );
}
// Tile light/transparency: 3D
lit_level map::light_at( const tripoint &p ) const
{
if( !inbounds( p ) ) {
return lit_level::DARK; // Out of bounds
}
const auto &map_cache = get_cache_ref( p.z );
const auto &lm = map_cache.lm;
const auto &sm = map_cache.sm;
if( sm[p.x][p.y] >= LIGHT_SOURCE_BRIGHT ) {
return lit_level::BRIGHT;
}
const float max_light = lm[p.x][p.y].max();
if( max_light >= LIGHT_AMBIENT_LIT ) {
return lit_level::LIT;
}
if( max_light >= LIGHT_AMBIENT_LOW ) {
return lit_level::LOW;
}
return lit_level::DARK;
}
float map::ambient_light_at( const tripoint &p ) const
{
if( !inbounds( p ) ) {
return 0.0f;
}
return get_cache_ref( p.z ).lm[p.x][p.y].max();
}
bool map::is_transparent( const tripoint &p ) const
{
return light_transparency( p ) > LIGHT_TRANSPARENCY_SOLID;
}
float map::light_transparency( const tripoint &p ) const
{
return get_cache_ref( p.z ).transparency_cache[p.x][p.y];
}
// End of tile light/transparency
map::apparent_light_info map::apparent_light_helper( const level_cache &map_cache,
const tripoint &p )
{
const float vis = std::max( map_cache.seen_cache[p.x][p.y], map_cache.camera_cache[p.x][p.y] );
const bool obstructed = vis <= LIGHT_TRANSPARENCY_SOLID + 0.1;
auto is_opaque = [&map_cache]( point p ) {
return map_cache.transparency_cache[p.x][p.y] <= LIGHT_TRANSPARENCY_SOLID &&
get_player_character().pos().xy() != p;
};
const bool p_opaque = is_opaque( p.xy() );
float apparent_light;
if( p_opaque && vis > 0 ) {
// This is the complicated case. We want to check which quadrants the
// player can see the tile from, and only count light values from those
// quadrants.
struct offset_and_quadrants {
point offset;
std::array<quadrant, 2> quadrants;
};
static constexpr std::array<offset_and_quadrants, 8> adjacent_offsets = {{
{ point_south, {{ quadrant::SE, quadrant::SW }} },
{ point_north, {{ quadrant::NE, quadrant::NW }} },
{ point_east, {{ quadrant::SE, quadrant::NE }} },
{ point_south_east, {{ quadrant::SE, quadrant::SE }} },
{ point_north_east, {{ quadrant::NE, quadrant::NE }} },
{ point_west, {{ quadrant::SW, quadrant::NW }} },
{ point_south_west, {{ quadrant::SW, quadrant::SW }} },
{ point_north_west, {{ quadrant::NW, quadrant::NW }} },
}
};
four_quadrants seen_from( 0 );
for( const offset_and_quadrants &oq : adjacent_offsets ) {
const point neighbour = p.xy() + oq.offset;
if( !lightmap_boundaries.contains( neighbour ) ) {
continue;
}
if( is_opaque( neighbour ) ) {
continue;
}
if( map_cache.seen_cache[neighbour.x][neighbour.y] == 0 &&
map_cache.camera_cache[neighbour.x][neighbour.y] == 0 ) {
continue;
}
// This is a non-opaque visible neighbour, so count visibility from the relevant
// quadrants
seen_from[oq.quadrants[0]] = vis;
seen_from[oq.quadrants[1]] = vis;
}
apparent_light = ( seen_from * map_cache.lm[p.x][p.y] ).max();
} else {
// This is the simple case, for a non-opaque tile light from all
// directions is equivalent
apparent_light = vis * map_cache.lm[p.x][p.y].max();
}
return { obstructed, apparent_light };
}
lit_level map::apparent_light_at( const tripoint &p, const visibility_variables &cache ) const
{
const int dist = rl_dist( g->u.pos(), p );
// Clairvoyance overrides everything.
if( dist <= cache.u_clairvoyance ) {
return lit_level::BRIGHT;
}
const auto &map_cache = get_cache_ref( p.z );
const apparent_light_info a = apparent_light_helper( map_cache, p );
// Unimpaired range is an override to strictly limit vision range based on various conditions,
// but the player can still see light sources.
if( dist > g->u.unimpaired_range() ) {
if( !a.obstructed && map_cache.sm[p.x][p.y] > 0.0 ) {
return lit_level::BRIGHT_ONLY;
} else {
return lit_level::DARK;
}
}
if( a.obstructed ) {
if( a.apparent_light > LIGHT_AMBIENT_LIT ) {
if( a.apparent_light > cache.g_light_level ) {
// This represents too hazy to see detail,
// but enough light getting through to illuminate.
return lit_level::BRIGHT_ONLY;
} else {
// If it's not brighter than the surroundings, it just ends up shadowy.
return lit_level::LOW;
}
} else {
return lit_level::BLANK;
}
}
// Then we just search for the light level in descending order.
if( a.apparent_light > LIGHT_SOURCE_BRIGHT || map_cache.sm[p.x][p.y] > 0.0 ) {
return lit_level::BRIGHT;
}
if( a.apparent_light > LIGHT_AMBIENT_LIT ) {
return lit_level::LIT;
}
if( a.apparent_light >= cache.vision_threshold ) {
return lit_level::LOW;
} else {
return lit_level::BLANK;
}
}
bool map::pl_sees( const tripoint &t, const int max_range ) const
{
if( !inbounds( t ) ) {
return false;
}
if( max_range >= 0 && square_dist( t, g->u.pos() ) > max_range ) {
return false; // Out of range!
}
const auto &map_cache = get_cache_ref( t.z );
const apparent_light_info a = apparent_light_helper( map_cache, t );
const float light_at_player = map_cache.lm[g->u.posx()][g->u.posy()].max();
return !a.obstructed &&
( a.apparent_light >= g->u.get_vision_threshold( light_at_player ) ||
map_cache.sm[t.x][t.y] > 0.0 );
}
bool map::pl_line_of_sight( const tripoint &t, const int max_range ) const
{
if( !inbounds( t ) ) {
return false;
}
if( max_range >= 0 && square_dist( t, g->u.pos() ) > max_range ) {
// Out of range!
return false;
}
const auto &map_cache = get_cache_ref( t.z );
// Any epsilon > 0 is fine - it means lightmap processing visited the point
return map_cache.seen_cache[t.x][t.y] > 0.0f ||
map_cache.camera_cache[t.x][t.y] > 0.0f;
}
//This algorithm is highly inaccurate and only suitable for the low (<60) values use in shadowcasting
//A starting constant of 21 and 4 iterations matches 2d 60^2. 16 and 5 matches 3d 60^3.
template <int start, int iterations>
static inline int fast_rl_dist( tripoint to )
{
if( !trigdist ) {
return square_dist( tripoint_zero, to );
}
int val = to.x * to.x + to.y * to.y + to.z * to.z;
if( val < 2 ) {
return val;
}
int a = start;
for( int i = 0; i < iterations; i++ ) {
int b = val / a;
a = ( a + b ) / 2;
}
return a;
}
// For a direction vector defined by x, y, return the quadrant that's the
// source of that direction. Assumes x != 0 && y != 0
// NOLINTNEXTLINE(cata-xy)
static constexpr quadrant quadrant_from_x_y( int x, int y )
{
return ( x > 0 ) ?
( ( y > 0 ) ? quadrant::NW : quadrant::SW ) :
( ( y > 0 ) ? quadrant::NE : quadrant::SE );
}
// Add defaults for when method is invoked for the first time.
template<int xx, int xy, int xz, int yx, int yy, int yz, int zz, typename T,
T( *calc )( const T &, const T &, const int & ),
bool( *check )( const T &, const T & ),
T( *accumulate )( const T &, const T &, const int & )>
void cast_zlight_segment(
const array_of_grids_of<T> &output_caches,
const array_of_grids_of<const T> &input_arrays,
const array_of_grids_of<const bool> &floor_caches,
const array_of_grids_of <const diagonal_blocks> &blocked_caches,
const tripoint &offset, int offset_distance,
T numerator = 1.0f, int row = 1,
float start_major = 0.0f, float end_major = 1.0f,
float start_minor = 0.0f, float end_minor = 1.0f,
T cumulative_transparency = LIGHT_TRANSPARENCY_OPEN_AIR,
int x_skip = -1, int z_skip = -1 );
template<int xx, int xy, int xz, int yx, int yy, int yz, int zz, typename T,
T( *calc )( const T &, const T &, const int & ),
bool( *check )( const T &, const T & ),
T( *accumulate )( const T &, const T &, const int & )>
void cast_zlight_segment(
const array_of_grids_of<T> &output_caches,
const array_of_grids_of<const T> &input_arrays,
const array_of_grids_of<const bool> &floor_caches,
const array_of_grids_of < const diagonal_blocks > &blocked_caches,
const tripoint &offset, const int offset_distance,
const T numerator, const int row,
float start_major, const float end_major,
float start_minor, float end_minor,
T cumulative_transparency, int x_skip, int z_skip )
{
if( start_major >= end_major || start_minor > end_minor ) {
return;
}
constexpr quadrant quad = quadrant_from_x_y( xx + xy, yx + yy );
const auto check_blocked = [ =, &blocked_caches]( const tripoint & p ) -> bool{
switch( quad )
{
case quadrant::NW:
return ( *blocked_caches[p.z + OVERMAP_DEPTH] )[p.x][p.y].nw;
break;
case quadrant::NE:
return ( *blocked_caches[p.z + OVERMAP_DEPTH] )[p.x][p.y].ne;
break;
case quadrant::SE:
return ( p.x < MAPSIZE_X - 1 && p.y < MAPSIZE_Y - 1 &&
( *blocked_caches[p.z + OVERMAP_DEPTH] )[p.x + 1][p.y + 1].nw );
break;
case quadrant::SW:
return ( p.x > 1 && p.y < MAPSIZE_Y - 1 &&
( *blocked_caches[p.z + OVERMAP_DEPTH] )[p.x - 1][p.y + 1].ne );
break;
}
cata::unreachable();
};
int radius = 60 - offset_distance;
constexpr int min_z = -OVERMAP_DEPTH;
constexpr int max_z = OVERMAP_HEIGHT;
T last_intensity = 0.0f;
tripoint delta;
tripoint current;
for( int distance = row; distance <= radius; distance++ ) {
delta.y = distance;
bool started_block = false;
T current_transparency = 0.0f;
bool current_floor = false;
int z_start = z_skip != -1 ? z_skip : std::max( 0,
static_cast<int>( std::ceil( ( ( distance - 0.5f ) * start_major ) - 0.5f ) ) );
int z_limit = std::min( distance,
static_cast<int>( std::ceil( ( ( distance + 0.5f ) * end_major ) + 0.5f ) ) - 1 );
for( delta.z = z_start; delta.z <= std::min( fov_3d_z_range, z_limit ); delta.z++ ) {
current.z = offset.z + delta.x * 00 + delta.y * 00 + delta.z * zz;
if( current.z > max_z || current.z < min_z ) {
continue;
}
const int z_index = current.z + OVERMAP_DEPTH;
int x_start = x_skip != -1 ? x_skip : std::max( 0,
static_cast<int>( std::ceil( ( ( distance - 0.5f ) * start_minor ) - 0.5f ) ) );
int x_limit = std::min( distance,
static_cast<int>( std::ceil( ( ( distance + 0.5f ) * end_minor ) + 0.5f ) ) - 1 );
for( delta.x = x_start; delta.x <= x_limit; delta.x++ ) {
current.x = offset.x + delta.x * xx + delta.y * xy + delta.z * xz;
current.y = offset.y + delta.x * yx + delta.y * yy + delta.z * yz;
if( !( current.x >= 0 && current.y >= 0 &&
current.x < MAPSIZE_X &&
current.y < MAPSIZE_Y ) ) {
continue;
}
if( check_blocked( current ) ) {
//We can just ignore the sliver of light that goes through vehicle holes
return;
}
T new_transparency = ( *input_arrays[z_index] )[current.x][current.y];
bool new_floor;
if( zz < 0 ) {
//Going down it's the current level's floor
new_floor = ( *floor_caches[z_index] )[current.x][current.y];
} else {
//Going up it's the next level's floor
new_floor = z_index < OVERMAP_LAYERS - 1 ? ( *floor_caches[z_index + 1] )[current.x][current.y] :
false;
}
if( !started_block ) {