From eac69ada2be42fb8da7248d8e298b1d9e795364a Mon Sep 17 00:00:00 2001 From: Kevin Granade Date: Mon, 10 Jun 2019 05:42:50 +0000 Subject: [PATCH 1/2] Only recalculate seen cache when necessary. --- src/lightmap.cpp | 5 +++-- src/map.cpp | 18 ++++++++++++------ src/map.h | 8 ++++++-- 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/lightmap.cpp b/src/lightmap.cpp index 9c7b9ee99da2d..552ed6302b56a 100644 --- a/src/lightmap.cpp +++ b/src/lightmap.cpp @@ -80,14 +80,14 @@ void map::add_light_from_items( const tripoint &p, std::list::iterator beg } // TODO: Consider making this just clear the cache and dynamically fill it in as trans() is called -void map::build_transparency_cache( const int zlev ) +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 ) { - return; + return false; } // Default to just barely not transparent. @@ -167,6 +167,7 @@ void map::build_transparency_cache( const int zlev ) } } map_cache.transparency_cache_dirty = false; + return true; } void map::apply_character_light( player &p ) diff --git a/src/map.cpp b/src/map.cpp index 917c005b1036f..7b04df7af08c1 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -7782,11 +7782,11 @@ void map::build_obstacle_cache( const tripoint &start, const tripoint &end, } } -void map::build_floor_cache( const int zlev ) +bool map::build_floor_cache( const int zlev ) { auto &ch = get_cache( zlev ); if( !ch.floor_cache_dirty ) { - return; + return false; } auto &floor_cache = ch.floor_cache; @@ -7812,6 +7812,7 @@ void map::build_floor_cache( const int zlev ) } ch.floor_cache_dirty = false; + return zlevels; } void map::build_floor_caches() @@ -7827,10 +7828,11 @@ void map::build_map_cache( const int zlev, bool skip_lightmap ) { const int minz = zlevels ? -OVERMAP_DEPTH : zlev; const int maxz = zlevels ? OVERMAP_HEIGHT : zlev; + bool seen_cache_dirty = false; for( int z = minz; z <= maxz; z++ ) { build_outside_cache( z ); - build_transparency_cache( z ); - build_floor_cache( z ); + seen_cache_dirty |= build_transparency_cache( z ); + seen_cache_dirty |= build_floor_cache( z ); } tripoint start( 0, 0, minz ); @@ -7877,10 +7879,14 @@ void map::build_map_cache( const int zlev, bool skip_lightmap ) const tripoint &p = g->u.pos(); if( ( has_furn( p ) && !furn( p ).obj().transparent ) || !ter( p ).obj().transparent ) { get_cache( p.z ).transparency_cache[p.x][p.y] = LIGHT_TRANSPARENCY_CLEAR; - set_transparency_cache_dirty( p.z ); } - build_seen_cache( g->u.pos(), zlev ); + // Initial value is illegal player position. + static tripoint player_prev_pos = tripoint_zero; + if( seen_cache_dirty || player_prev_pos != p ) { + build_seen_cache( g->u.pos(), zlev ); + player_prev_pos = p; + } if( !skip_lightmap ) { generate_lightmap( zlev ); } diff --git a/src/map.h b/src/map.h index cbb3c5927cc96..8c7bbc5521369 100644 --- a/src/map.h +++ b/src/map.h @@ -1463,11 +1463,15 @@ class map void draw_connections( const oter_id &terrain_type, mapgendata &dat, const time_point &when, const float density ); - void build_transparency_cache( int zlev ); + // Builds a transparency cache and returns true if the cache was invalidated. + // Used to determine if seen cache should be rebuilt. + bool build_transparency_cache( int zlev ); void build_sunlight_cache( int zlev ); public: void build_outside_cache( int zlev ); - void build_floor_cache( int zlev ); + // Builds a floor cache and returns true if the cache was invalidated. + // Used to determine if seen cache should be rebuilt. + bool build_floor_cache( int zlev ); // We want this visible in `game`, because we want it built earlier in the turn than the rest void build_floor_caches(); From 951503fbc601e22a0babe1f61ddf0a9e8d71a8a3 Mon Sep 17 00:00:00 2001 From: Kevin Granade Date: Mon, 10 Jun 2019 05:43:15 +0000 Subject: [PATCH 2/2] Remove some unecessary cache invalidations --- src/game.cpp | 2 -- src/handle_action.cpp | 1 - 2 files changed, 3 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index 6618bbd7d7dfc..474a0b9403c36 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1501,7 +1501,6 @@ bool game::do_turn() sounds::process_sounds(); // Update vision caches for monsters. If this turns out to be expensive, // consider a stripped down cache just for monsters. - m.invalidate_map_cache( get_levz() ); m.build_map_cache( get_levz(), true ); monmove(); if( calendar::once_every( 3_minutes ) ) { @@ -3148,7 +3147,6 @@ void game::draw() //temporary fix for updating visibility for minimap ter_view_z = ( u.pos() + u.view_offset ).z; - m.invalidate_map_cache( ter_view_z ); m.build_map_cache( ter_view_z ); m.update_visibility_cache( ter_view_z ); diff --git a/src/handle_action.cpp b/src/handle_action.cpp index 4d3d94f7a4f1d..9b8189e5a3ec0 100644 --- a/src/handle_action.cpp +++ b/src/handle_action.cpp @@ -1951,7 +1951,6 @@ bool game::handle_action() add_msg( m_info, _( "You can't disassemble items while you're riding." ) ); } else { u.disassemble(); - g->m.invalidate_map_cache( g->get_levz() ); refresh_all(); } break;