Build light per level

- sunlight is built into its own grid per level; each level's light
  starts from it and adds only the sources standing on that level
- building one level no longer writes over another level's light,
  such as its darkness fields
- levels the avatar or a camera sees get full light; the rest get
  sunlight alone, as all levels did before
- open sky above the highest structure is one value, not a full grid
This commit is contained in:
dobbry-vechur
2026-10-05 13:54:21 +04:00
parent c602aca9aa
commit ee76be710f
8 changed files with 428 additions and 44 deletions
+4
View File
@@ -42,6 +42,10 @@ void level_cache::clear()
transparency_vehicle_opaque_any = false;
sight_revision = next_cache_generation();
geometry_revision = next_cache_generation();
sun_revision = next_cache_generation();
sun_uniform = -1.0f;
light_full = false;
lightmap_sun_revision = 0;
no_floor_gaps = false;
natural_light_level_cache = 0.0f;
+15 -2
View File
@@ -29,6 +29,9 @@ uint64_t next_cache_generation();
// in a single call instead of a bunch of fill_n which don't optimize well.
struct level_cache_default_zero_members {
cata::mdarray<four_quadrants, point_bub_ms> lm;
// sunlight alone, which every level's lm starts from; only the sunlight
// pass writes it
cata::mdarray<four_quadrants, point_bub_ms> sun_lm;
cata::mdarray<float, point_bub_ms> sm;
// Accumulated colored light energy per tile. Populated during generate_lightmap
// alongside lm/sm. Zero = uncolored (white) light only.
@@ -133,9 +136,19 @@ struct level_cache : level_cache_default_zero_members {
bool floor_cache_dirty = false;
bool seen_cache_dirty = false;
bool lightmap_dirty = true;
// set from next_cache_generation each time generate_lightmap rebuilds
// lm and sm; a reader compares it to tell the light changed
// set from next_cache_generation each time the level's light grids are
// replaced, by a full build or by sunlight alone; a reader compares it
// to tell the light changed
uint64_t lightmap_generation = 0;
// set from next_cache_generation when the sunlight pass rewrites sun_lm
uint64_t sun_revision = 0;
// lm also holds the level's light sources, not just sunlight
bool light_full = false;
// light of every tile when the level lies above all populated ones and
// the sunlight pass left sun_lm unwritten; negative otherwise
float sun_uniform = -1.0f;
// sun_revision of the sun_lm lm was last built from
uint64_t lightmap_sun_revision = 0;
// set from next_cache_generation each time update_visibility_cache
// recomputes visibility_cache
uint64_t visibility_generation = 0;
+78 -26
View File
@@ -379,18 +379,22 @@ bool map::build_vision_transparency_cache( int zlev )
void map::apply_character_light( Character &p )
{
const tripoint_bub_ms pos = p.pos_bub( *this );
if( !inbounds( pos ) ) {
return;
}
if( p.has_effect( effect_onfire ) ) {
apply_light_source( p.pos_bub(), 8 );
apply_light_source( pos, 8 );
} else if( p.has_effect( effect_haslight ) ) {
apply_light_source( p.pos_bub(), 4 );
apply_light_source( pos, 4 );
}
const float held_luminance = p.active_light();
if( held_luminance > LIGHT_AMBIENT_LOW ) {
apply_light_source( p.pos_bub(), held_luminance );
apply_light_source( pos, held_luminance );
}
if( held_luminance >= 4 && held_luminance > ambient_light_at( p.pos_bub() ) - 0.5f ) {
if( held_luminance >= 4 && held_luminance > ambient_light_at( pos ) - 0.5f ) {
p.add_effect( effect_haslight, 1_turns );
}
}
@@ -399,13 +403,19 @@ void map::apply_character_light( Character &p )
// 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 )
void map::build_sunlight_cache()
{
const int zlev_min = -OVERMAP_DEPTH;
// Start at the topmost populated zlevel to avoid unnecessary raycasting
// Plus one zlevel to prevent clipping inside structures
const int zlev_max = clamp( calc_max_populated_zlev() + 1, std::min( pzlev + 1, OVERMAP_HEIGHT ),
OVERMAP_HEIGHT );
const int zlev_max = std::min( calc_max_populated_zlev() + 1, OVERMAP_HEIGHT );
// levels above that are open sky: one value each, no grid to fill
for( int zlev = OVERMAP_HEIGHT; zlev > zlev_max; zlev-- ) {
level_cache &map_cache = get_cache( zlev );
map_cache.natural_light_level_cache = g->natural_light_level( zlev );
map_cache.sun_revision = next_cache_generation();
map_cache.sun_uniform = g->natural_light_level( 0 );
}
// true if all previous z-levels are fully transparent to light (no floors, transparency >= air)
bool fully_outside = true;
@@ -422,14 +432,11 @@ void map::build_sunlight_cache( int pzlev )
// Iterate top to bottom because sunlight cache needs to construct in that order.
for( int zlev = zlev_max; zlev >= zlev_min; zlev-- ) {
if( pzlev != get_avatar().posz() && zlev == get_avatar().posz() ) {
// Don't trash the lighting for the PC when this is called for someone else at a different
// Z level, as only the specified Z level is being rebuilt with light sources by the caller.
continue;
}
level_cache &map_cache = get_cache( zlev );
map_cache.natural_light_level_cache = g->natural_light_level( zlev );
auto &lm = map_cache.lm;
map_cache.sun_revision = next_cache_generation();
map_cache.sun_uniform = -1.0f;
auto &lm = map_cache.sun_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()
@@ -471,7 +478,7 @@ void map::build_sunlight_cache( int pzlev )
// 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.
const level_cache &prev_map_cache = get_cache_ref( zlev + 1 );
const auto &prev_lm = prev_map_cache.lm;
const auto &prev_lm = prev_map_cache.sun_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;
@@ -536,21 +543,56 @@ void map::build_sunlight_cache( int pzlev )
}
}
void map::ensure_light( const int zlev ) const
{
// light built from the current sunlight stays valid as of the last
// build_map_cache, full or not, like the views
if( get_cache_ref( zlev ).lightmap_sun_revision != get_cache_ref( zlev ).sun_revision ) {
set_sunlight_only( zlev );
}
}
void map::set_sunlight_only( const int zlev ) const
{
// a level no cast reaches gets sunlight alone, as every level did before
// light was built per level; build_map_cache adds the sources where a cast
// reached
level_cache &ch = get_cache( zlev );
if( ch.sun_uniform >= 0.0f ) {
ch.lm.fill( four_quadrants( ch.sun_uniform ) );
} else {
ch.lm = ch.sun_lm;
}
ch.sm.fill( 0 );
ch.light_color_cache.fill( light_color_rgb{} );
ch.has_colored_lights = false;
ch.light_full = false;
ch.lightmap_sun_revision = ch.sun_revision;
ch.lightmap_generation = next_cache_generation();
}
void map::generate_lightmap( const int zlev )
{
level_cache &map_cache = get_cache( zlev );
if( !map_cache.lightmap_dirty ) {
if( map_cache.light_full && !map_cache.lightmap_dirty &&
map_cache.lightmap_sun_revision == map_cache.sun_revision ) {
return;
}
map_cache.light_full = true;
map_cache.lightmap_dirty = false;
map_cache.lightmap_generation = next_cache_generation();
map_cache.lightmap_sun_revision = map_cache.sun_revision;
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;
lm.fill( four_quadrants{} );
// artificial light goes on top of what the sunlight pass left
if( map_cache.sun_uniform >= 0.0f ) {
lm.fill( four_quadrants( map_cache.sun_uniform ) );
} else {
lm = map_cache.sun_lm;
}
sm.fill( 0 );
map_cache.light_color_cache.fill( light_color_rgb{} );
map_cache.has_colored_lights = false;
@@ -580,8 +622,6 @@ void map::generate_lightmap( const int zlev )
const float natural_light = g->natural_light_level( zlev );
build_sunlight_cache( zlev );
// Dawn/dusk tint: color sunlit tiles during twilight. At this point lm
// contains only sunlight (no artificial sources yet), so any excess over
// the indoor baseline is sunlight that reached the tile.
@@ -618,9 +658,17 @@ void map::generate_lightmap( const int zlev )
}
}
apply_character_light( get_player_character() );
// each source lights only the level it stands on, when that level is built;
// light written here into another level would land after that level's
// overrides and outlive its publication
Character &you = get_player_character();
if( you.posz() == zlev ) {
apply_character_light( you );
}
for( npc &guy : g->all_npcs() ) {
apply_character_light( guy );
if( guy.posz() == zlev ) {
apply_character_light( guy );
}
}
std::vector<std::pair<tripoint_bub_ms, float>> lm_override;
@@ -698,8 +746,8 @@ void map::generate_lightmap( const int zlev )
if( critter.is_hallucination() ) {
continue;
}
const tripoint_bub_ms mp = critter.pos_bub();
if( inbounds( mp ) ) {
const tripoint_bub_ms mp = critter.pos_bub( *this );
if( inbounds( mp ) && mp.z() == zlev ) {
if( critter.has_effect( effect_onfire ) ) {
apply_light_source( mp, 8 );
}
@@ -737,7 +785,7 @@ void map::generate_lightmap( const int zlev )
const vpart_info &vp = pt->info();
tripoint_bub_ms src = v->bub_part_pos( *this, *pt );
if( !inbounds( src ) ) {
if( !inbounds( src ) || src.z() != zlev ) {
continue;
}
@@ -787,7 +835,7 @@ void map::generate_lightmap( const int zlev )
for( const vpart_reference &vpr : v->get_any_parts( VPFLAG_CARGO ) ) {
const tripoint_bub_ms pos = vpr.pos_bub( *this );
if( !inbounds( pos ) || vpr.info().has_flag( "COVERED" ) ) {
if( !inbounds( pos ) || pos.z() != zlev || vpr.info().has_flag( "COVERED" ) ) {
continue;
}
add_light_from_items( pos, vpr.items() );
@@ -843,6 +891,8 @@ void map::generate_lightmap( const int zlev )
}
}
}
// only place a level's light is complete, so the only one that publishes it
map_cache.lightmap_generation = next_cache_generation();
}
void map::add_light_source( const tripoint_bub_ms &p, float luminance,
@@ -897,6 +947,7 @@ lit_level map::light_at( const tripoint_bub_ms &p ) const
if( !inbounds( p ) ) {
return lit_level::DARK; // Out of bounds
}
ensure_light( p.z() );
const level_cache &map_cache = get_cache_ref( p.z() );
const auto &lm = map_cache.lm;
@@ -922,7 +973,7 @@ float map::ambient_light_at( const tripoint_bub_ms &p ) const
if( !this->inbounds( p ) ) {
return 0.0f;
}
ensure_light( p.z() );
return get_cache_ref( p.z() ).lm[p.x()][p.y()].max();
}
@@ -1090,6 +1141,7 @@ bool map::pl_sees( const tripoint_bub_ms &t, const int max_range ) const
if( !inbounds( t ) ) {
return false;
}
ensure_light( t.z() );
const level_cache &map_cache = get_cache_ref( t.z() );
Character &player_character = get_player_character();
+86 -2
View File
@@ -11017,9 +11017,84 @@ void map::build_map_cache( const int zlev, bool skip_lightmap )
}
}
if( !skip_lightmap ) {
generate_lightmap( zlev );
if( seen_cache_dirty ) {
refresh_vision_levels();
}
if( !skip_lightmap ) {
update_sunlight();
generate_lightmap( zlev );
for( const int z : vision_levels_list ) {
generate_lightmap( z );
}
// a level this build didn't light drops the sources it held, which
// nothing would rebuild while its sunlight stays the same
for( int z = -OVERMAP_DEPTH; z <= OVERMAP_HEIGHT; ++z ) {
if( get_cache_ref( z ).light_full && z != zlev &&
std::find( vision_levels_list.begin(), vision_levels_list.end(), z ) == vision_levels_list.end() ) {
set_sunlight_only( z );
}
}
}
}
int map::lowest_vision_level( const int zlev ) const
{
// avatar's view reaches fov_3d_z_range levels down from its own
const int avatar_z = get_avatar().posz();
return std::max( std::min( zlev, avatar_z - fov_3d_z_range ), -OVERMAP_DEPTH );
}
bool map::level_reached_by_cast( const int zlev ) const
{
const level_cache &ch = get_cache_ref( zlev );
for( int x = 0; x < MAPSIZE_X; ++x ) {
for( int y = 0; y < MAPSIZE_Y; ++y ) {
if( ch.seen_cache[x][y] > 0.0f || ch.camera_cache[x][y] > 0.0f ) {
return true;
}
}
}
return false;
}
void map::refresh_vision_levels()
{
const int avatar_z = get_avatar().posz();
vision_levels_list.assign( 1, avatar_z );
// a level below the avatar's that no cast reaches reads no light, since
// final visibility there is blank whatever its lightmap holds
for( int z = avatar_z - 1; z >= lowest_vision_level( avatar_z ); --z ) {
if( level_reached_by_cast( z ) ) {
vision_levels_list.push_back( z );
}
}
}
const std::vector<int> &map::vision_levels() const
{
return vision_levels_list;
}
void map::update_sunlight()
{
std::vector<float> natural_light;
natural_light.reserve( OVERMAP_HEIGHT + 1 );
for( int z = 0; z <= OVERMAP_HEIGHT; ++z ) {
natural_light.push_back( g->natural_light_level( z ) );
}
std::vector<uint64_t> revisions;
revisions.reserve( 2 * OVERMAP_LAYERS );
for( int z = -OVERMAP_DEPTH; z <= OVERMAP_HEIGHT; ++z ) {
const level_cache &ch = get_cache_ref( z );
revisions.push_back( ch.sight_revision );
revisions.push_back( ch.geometry_revision );
}
if( natural_light == sunlight_natural_light && revisions == sunlight_scene_revisions ) {
return;
}
build_sunlight_cache();
sunlight_natural_light = std::move( natural_light );
sunlight_scene_revisions = std::move( revisions );
}
void map::rebuild_vision_caches_from_scratch( const int zlev )
@@ -11038,10 +11113,19 @@ void map::rebuild_vision_caches_from_scratch( const int zlev )
avatar_fov_range = -1;
avatar_fov_vision_parts.clear();
camera_fov_moncams.clear();
sunlight_scene_revisions.clear();
cached_char_lights.clear();
fov_scene_revisions = {};
g->reset_light_level();
build_map_cache( zlev );
// light for every level final visibility reads, whatever build_map_cache
// chose to refresh
refresh_vision_levels();
update_sunlight();
for( const int z : vision_levels_list ) {
get_cache( z ).lightmap_dirty = true;
generate_lightmap( z );
}
invalidate_visibility_cache();
update_visibility_cache( zlev );
}
+28 -3
View File
@@ -1845,11 +1845,16 @@ class map
void apply_faction_ownership( const point_bub_ms &p1, const point_bub_ms &p2,
const faction_id &id );
void do_vehicle_caching( int z );
// Note: in 3D mode, will actually build caches on ALL z-levels
// Note: in 3D mode, builds scene and view caches on ALL z-levels, and light
// on zlev and the levels final visibility reads
void build_map_cache( int zlev, bool skip_lightmap = false );
// rebuilds every vision cache from nothing; incremental build of same
// scene must match it
void rebuild_vision_caches_from_scratch( int zlev );
// avatar's level, then each level below it within fov_3d_z_range that the
// avatar or a camera sees part of, as of the last cast; build_map_cache
// keeps their light current
const std::vector<int> &vision_levels() const;
// Unlike the other caches, this populates a supplied cache instead of an internal cache.
void build_obstacle_cache(
const tripoint_bub_ms &start, const tripoint_bub_ms &end,
@@ -2091,8 +2096,23 @@ class map
// cover cells the avatar's posture overrides in vision_transparency_cache
// on level zlev, with the value each takes
std::vector<std::pair<point_bub_ms, float>> observer_vision_overrides( int zlev ) const;
// fills lm with sunlight. pzlev is current player's zlevel
void build_sunlight_cache( int pzlev );
// fills sun_lm of every level up to the highest populated one, and
// sun_uniform of the open sky above
void build_sunlight_cache();
// rebuilds sun_lm when natural light or the scene changed
void update_sunlight();
// gives level zlev sunlight alone when its light predates the last
// sunlight pass; light_at, ambient_light_at, pl_sees and
// update_visibility_cache call it, so a level build_map_cache did not
// light holds sunlight, never zeros
void ensure_light( int zlev ) const;
// replaces level zlev's light with its sunlight alone and publishes it
void set_sunlight_only( int zlev ) const;
void refresh_vision_levels();
// lowest level final visibility reads for a request at zlev
int lowest_vision_level( int zlev ) const;
// avatar's or a camera's cast reached a tile of level zlev
bool level_reached_by_cast( int zlev ) const;
public:
void build_outside_cache( int zlev );
// Get a bitmap indicating which layers are potentially visible from the target layer.
@@ -2408,6 +2428,11 @@ class map
}
};
std::vector<camera_fov_input> camera_fov_moncams;
// what the last sunlight pass read
std::vector<float> sunlight_natural_light;
std::vector<uint64_t> sunlight_scene_revisions;
// levels whose light final visibility reads; see vision_levels
std::vector<int> vision_levels_list;
// light and position of every character as last lightmap saw them
std::vector<std::pair<float, tripoint_bub_ms>> cached_char_lights;
// sight and geometry revisions of each level as last avatar cast saw them
+36 -9
View File
@@ -19,8 +19,10 @@
#include "coordinates.h"
#include "creature_tracker.h"
#include "game.h"
#include "game_constants.h"
#include "item.h"
#include "level_cache.h"
#include "lightmap.h"
#include "map.h"
#include "map_helpers.h"
#include "map_iterator.h"
@@ -169,8 +171,10 @@ struct vision_cache_snapshot {
std::map<int, std::unique_ptr<level_cache_default_zero_members>> levels;
// per pair: optical and physical trace, each with and without fields
std::vector<std::array<bool, 4>> sees;
std::vector<int> vision_levels;
uint64_t seen_generation = 0;
std::map<int, std::pair<uint64_t, uint64_t>> level_generations;
std::map<int, bool> has_colored_lights;
};
} // namespace
@@ -187,16 +191,22 @@ static vision_cache_snapshot snapshot_vision_caches( const los_pairs &pairs )
{
const map &here = get_map();
vision_cache_snapshot snap;
for( int z = -OVERMAP_DEPTH; z <= OVERMAP_HEIGHT; ++z ) {
// any reader may ask any level for light, which builds it on demand
here.light_at( tripoint_bub_ms( 0, 0, z ) );
}
for( int z = -OVERMAP_DEPTH; z <= OVERMAP_HEIGHT; ++z ) {
const level_cache &ch = here.access_cache( z );
snap.levels.emplace( z, std::make_unique<level_cache_default_zero_members>( ch ) );
snap.level_generations.emplace( z, std::make_pair( ch.lightmap_generation,
ch.visibility_generation ) );
snap.has_colored_lights.emplace( z, ch.has_colored_lights );
}
for( const std::pair<tripoint_bub_ms, tripoint_bub_ms> &pr : pairs ) {
snap.sees.push_back( sees_all_traces( here, pr.first, pr.second ) );
}
snap.seen_generation = here.seen_generation();
snap.vision_levels = here.vision_levels();
return snap;
}
@@ -219,12 +229,14 @@ static void check_layer_matches( const std::string &layer, const int z, const Ge
CHECK( mismatches == 0 );
}
// other levels' illumination isn't rebuilt on request, so light and final
// classification are only compared on avatar's level
// light is compared on every level; final classification on the levels a
// request at zlev reads, from the avatar's view range down
static void check_snapshots_match( const vision_cache_snapshot &a, const vision_cache_snapshot &b,
const vision_layers layers )
const vision_layers layers, const int zlev )
{
const int avatar_z = get_avatar().posz();
const int lowest_read = std::max( std::min( zlev, get_avatar().posz() - fov_3d_z_range ),
-OVERMAP_DEPTH );
CHECK( a.vision_levels == b.vision_levels );
for( const auto &[z, la] : a.levels ) {
const auto found = b.levels.find( z );
if( found == b.levels.end() ) {
@@ -259,7 +271,7 @@ static void check_snapshots_match( const vision_cache_snapshot &a, const vision_
check_layer_matches( "camera", z, [&]( int i, int j ) {
return x.camera_cache[i][j] == y.camera_cache[i][j];
} );
if( layers != vision_layers::all || z != avatar_z ) {
if( layers == vision_layers::scene_and_fov ) {
continue;
}
check_layer_matches( "lm", z, [&]( int i, int j ) {
@@ -268,6 +280,16 @@ static void check_snapshots_match( const vision_cache_snapshot &a, const vision_
check_layer_matches( "sm", z, [&]( int i, int j ) {
return x.sm[i][j] == y.sm[i][j];
} );
check_layer_matches( "light_color", z, [&]( int i, int j ) {
const light_color_rgb &cx = x.light_color_cache[i][j];
const light_color_rgb &cy = y.light_color_cache[i][j];
return cx.r == cy.r && cx.g == cy.g && cx.b == cy.b;
} );
CAPTURE( z );
CHECK( a.has_colored_lights.at( z ) == b.has_colored_lights.at( z ) );
if( layers != vision_layers::all || z < lowest_read || z > zlev ) {
continue;
}
check_layer_matches( "visibility", z, [&]( int i, int j ) {
return x.visibility_cache[i][j] == y.visibility_cache[i][j];
} );
@@ -286,12 +308,17 @@ void vision_cache_oracle::prime() const
}
void vision_cache_oracle::check_matches_rebuild( const vision_layers layers ) const
{
check_matches_rebuild( layers, get_avatar().posz() );
}
void vision_cache_oracle::check_matches_rebuild( const vision_layers layers, const int zlev ) const
{
const vision_cache_snapshot incremental = snapshot_vision_caches( pairs_ );
get_map().rebuild_vision_caches_from_scratch( get_avatar().posz() );
get_map().rebuild_vision_caches_from_scratch( zlev );
const vision_cache_snapshot rebuilt = snapshot_vision_caches( pairs_ );
CAPTURE( layers == vision_layers::all );
check_snapshots_match( incremental, rebuilt, layers );
CAPTURE( layers == vision_layers::all, zlev );
check_snapshots_match( incremental, rebuilt, layers, zlev );
}
void check_stationary_build_is_noop()
@@ -304,7 +331,7 @@ void check_stationary_build_is_noop()
here.build_map_cache( z );
here.update_visibility_cache( z );
const vision_cache_snapshot second = snapshot_vision_caches( {} );
check_snapshots_match( first, second, vision_layers::all );
check_snapshots_match( first, second, vision_layers::all, z );
CHECK( first.seen_generation == second.seen_generation );
CHECK( first.level_generations == second.level_generations );
}
+6 -2
View File
@@ -33,7 +33,9 @@ enum class vision_layers {
// outside, floor, transparency, sight, vision transparency, seen and
// camera caches, plus pairwise sight answers on both traces
scene_and_fov,
// also light and final visibility classification on avatar's level
// also light on every level, as a reader asking for it would find it
light,
// and final visibility classification on the levels the request reads
all,
};
@@ -46,8 +48,10 @@ class vision_cache_oracle
// asks every pair once, so pairwise caches hold answers a later mutation
// can leave stale
void prime() const;
// call after the incremental build following a mutation
// call after the incremental build following a mutation; final
// visibility is compared for a request at the avatar's level, or zlev
void check_matches_rebuild( vision_layers layers = vision_layers::all ) const;
void check_matches_rebuild( vision_layers layers, int zlev ) const;
private:
los_pairs pairs_;
};
+175
View File
@@ -1,4 +1,5 @@
#include <algorithm>
#include <array>
#include <cstdlib>
#include <functional>
#include <memory>
@@ -20,6 +21,7 @@
#include "current_map.h"
#include "enums.h"
#include "game.h"
#include "item.h"
#include "level_cache.h"
#include "lightmap.h"
#include "map.h"
@@ -35,6 +37,7 @@
#include "overmap_ui.h"
#include "player_helpers.h"
#include "point.h"
#include "shadowcasting.h"
#include "string_formatter.h"
#include "teleport.h"
#include "type_id.h"
@@ -47,14 +50,18 @@
static const efftype_id effect_narcosis( "narcosis" );
static const field_type_str_id field_fd_fire( "fd_fire" );
static const field_type_str_id field_fd_darkness( "fd_darkness" );
static const field_type_str_id field_fd_smoke( "fd_smoke" );
static const furn_str_id furn_f_chair( "f_chair" );
static const furn_str_id furn_test_f_translucent( "test_f_translucent" );
static const itype_id itype_glowstick_lit( "glowstick_lit" );
static const move_mode_id move_mode_crouch( "crouch" );
static const move_mode_id move_mode_walk( "walk" );
static const mtype_id mon_crow( "mon_crow" );
static const mtype_id mon_test_camera( "mon_test_camera" );
static const mtype_id mon_zombie( "mon_zombie" );
static const mtype_id mon_zombie_electric( "mon_zombie_electric" );
@@ -1520,6 +1527,174 @@ TEST_CASE( "vision_cache_observer_transitions_match_rebuild", "[vision]" )
}
}
// cellar the avatar looks down into through a hole beside it
static void build_transition_cellar( const tripoint_bub_ms &origin )
{
map &here = get_map();
const tripoint_bub_ms hole = origin + tripoint::east;
for( const tripoint_bub_ms &p : here.points_in_radius( hole + tripoint::below, 2 ) ) {
here.ter_set( p, ter_t_floor );
}
here.ter_set( hole, ter_t_open_air );
}
TEST_CASE( "vision_cache_light_transitions_match_rebuild", "[vision]" )
{
const tripoint_bub_ms origin{ 60, 60, 0 };
set_up_transition_scene( origin );
map &here = get_map();
calendar::turn = midnight;
g->reset_light_level();
build_transition_cellar( origin );
const vision_cache_oracle oracle( los_pairs_around( origin, 6 ) );
here.rebuild_vision_caches_from_scratch( 0 );
const std::vector<int> &levels = here.vision_levels();
REQUIRE( std::find( levels.begin(), levels.end(), -1 ) != levels.end() );
oracle.prime();
SECTION( "a_light_in_the_cellar" ) {
here.ter_set( origin + tripoint_rel_ms{ 2, 1, -1 }, ter_t_utility_light );
}
SECTION( "a_colored_light_in_the_cellar" ) {
REQUIRE( here.add_field( origin + tripoint_rel_ms{ 2, 1, -1 }, field_fd_fire, 2 ) );
}
SECTION( "the_night_ends" ) {
calendar::turn = day_time;
g->reset_light_level();
}
build_vision_caches_incrementally();
oracle.check_matches_rebuild( vision_layers::light );
}
TEST_CASE( "vision_light_does_not_depend_on_build_order", "[vision]" )
{
const tripoint_bub_ms origin{ 60, 60, 0 };
set_up_transition_scene( origin );
map &here = get_map();
calendar::turn = midnight;
g->reset_light_level();
build_transition_cellar( origin );
here.ter_set( origin + tripoint_rel_ms{ 2, 1, -1 }, ter_t_utility_light );
here.ter_set( origin + tripoint_rel_ms{ -2, 0, 0 }, ter_t_utility_light );
const auto light_after = [&]( const std::vector<int> &order ) {
here.rebuild_vision_caches_from_scratch( 0 );
for( const int z : order ) {
here.invalidate_map_cache( z );
here.build_map_cache( z );
}
using lightmaps = std::pair<cata::mdarray<four_quadrants, point_bub_ms>,
cata::mdarray<four_quadrants, point_bub_ms>>;
return std::make_unique<lightmaps>( here.access_cache( 0 ).lm, here.access_cache( -1 ).lm );
};
const auto down = light_after( { 0, -1 } );
const auto up = light_after( { -1, 0 } );
int mismatches = 0;
for( int x = 0; x < MAPSIZE_X; ++x ) {
for( int y = 0; y < MAPSIZE_Y; ++y ) {
mismatches += down->first[x][y].values != up->first[x][y].values;
mismatches += down->second[x][y].values != up->second[x][y].values;
}
}
CHECK( mismatches == 0 );
}
TEST_CASE( "vision_darkness_survives_light_built_for_a_lower_level", "[vision]" )
{
const tripoint_bub_ms origin{ 60, 60, 0 };
set_up_transition_scene( origin );
map &here = get_map();
calendar::turn = midnight;
g->reset_light_level();
build_transition_cellar( origin );
player_add_headlamp();
REQUIRE( here.add_field( origin, field_fd_darkness, 1 ) );
here.rebuild_vision_caches_from_scratch( 0 );
const std::vector<int> &levels = here.vision_levels();
REQUIRE( std::find( levels.begin(), levels.end(), -1 ) != levels.end() );
CHECK( here.access_cache( 0 ).lm[origin.xy()].max() == 0.0f );
}
TEST_CASE( "vision_light_is_current_on_every_level_a_reader_asks_about", "[vision]" )
{
const tripoint_bub_ms origin{ 60, 60, 0 };
set_up_transition_scene( origin );
map &here = get_map();
avatar &you = get_avatar();
GIVEN( "daylight over open ground, with nothing built above the avatar's level" ) {
here.rebuild_vision_caches_from_scratch( 0 );
WHEN( "crows fly one level up after an ordinary build" ) {
const tripoint_bub_ms sky = origin + tripoint_rel_ms{ 4, 0, 1 };
monster *const near = g->place_critter_at( mon_crow, sky );
monster *const far = g->place_critter_at( mon_crow, sky + tripoint_rel_ms{ 8, 0, 0 } );
REQUIRE( near != nullptr );
REQUIRE( far != nullptr );
build_vision_caches_incrementally();
REQUIRE( here.access_cache( 1 ).seen_cache[sky.xy()] > 0.0f );
THEN( "the sky there holds daylight" ) {
CHECK( here.ambient_light_at( sky ) > LIGHT_AMBIENT_LIT );
}
THEN( "avatar sees a crow there" ) {
CHECK( you.sees( here, *near ) );
}
THEN( "crows see each other" ) {
CHECK( near->sees( here, *far ) );
}
}
WHEN( "a level above the avatar's is built on request" ) {
const int z = GENERATE( 1, OVERMAP_HEIGHT );
CAPTURE( z );
const tripoint_bub_ms sky( origin.xy(), z );
here.build_map_cache( z );
REQUIRE( here.access_cache( z ).seen_cache[sky.xy()] > 0.0f );
THEN( "its sky holds natural light" ) {
CHECK( here.ambient_light_at( sky ) == Approx( g->natural_light_level( z ) ) );
}
}
WHEN( "final visibility is asked for two levels up" ) {
here.build_map_cache( 0 );
here.update_visibility_cache( 2 );
const tripoint_bub_ms between( origin.xy() + point::east, 1 );
REQUIRE( here.access_cache( 1 ).seen_cache[between.xy()] > 0.0f );
THEN( "the level between is lit, not dark" ) {
const lit_level ll = here.access_cache( 1 ).visibility_cache[between.xy()];
CAPTURE( static_cast<int>( ll ) );
CHECK( ( ll == lit_level::LIT || ll == lit_level::BRIGHT ) );
}
}
}
}
TEST_CASE( "vision_light_of_a_level_no_cast_reaches_drops_its_sources", "[vision]" )
{
const tripoint_bub_ms origin{ 60, 60, 0 };
set_up_transition_scene( origin );
map &here = get_map();
calendar::turn = midnight;
g->reset_light_level();
build_transition_cellar( origin );
// same submap as origin, so moving there shifts no map
const tripoint_bub_ms hideout = origin + tripoint_rel_ms{ 10, 0, 0 };
build_transition_room( hideout );
const tripoint_bub_ms lamp = origin + tripoint_rel_ms{ 2, 1, -1 };
here.add_item( lamp, item( itype_glowstick_lit ) );
const vision_cache_oracle oracle( los_pairs_around( origin, 6 ) );
GIVEN( "a cellar lit by a glowstick the avatar sees into" ) {
here.rebuild_vision_caches_from_scratch( 0 );
const std::vector<int> &levels = here.vision_levels();
REQUIRE( std::find( levels.begin(), levels.end(), -1 ) != levels.end() );
REQUIRE( here.access_cache( -1 ).light_full );
oracle.prime();
WHEN( "avatar steps where no cast reaches the cellar and the glowstick goes" ) {
g->place_player( hideout );
here.i_clear( lamp );
build_vision_caches_incrementally();
REQUIRE( std::find( levels.begin(), levels.end(), -1 ) == levels.end() );
THEN( "cellar's light is what a rebuild gives, with no glowstick" ) {
oracle.check_matches_rebuild( vision_layers::light );
}
}
}
}
static monster &spawn_transition_moncam( const tripoint_bub_ms &p )
{
monster *camera = g->place_critter_at( mon_test_camera, p );