From 027e63ecdceffeb6ef91e4aa43c79c50368a23c0 Mon Sep 17 00:00:00 2001 From: ZhilkinSerg Date: Tue, 30 Apr 2019 10:38:50 +0300 Subject: [PATCH] Map cache invalidation --- src/activity_handlers.cpp | 2 ++ src/bionics.cpp | 2 ++ src/game.cpp | 10 +++++++++- src/handle_action.cpp | 5 +---- src/map.h | 9 +++++++++ src/mapgen.cpp | 1 + src/ranged.cpp | 1 + src/start_location.cpp | 1 + src/vehicle_use.cpp | 1 + tests/crafting_test.cpp | 1 + tests/map_helpers.cpp | 1 + tests/vehicle_drag.cpp | 1 + tests/vehicle_efficiency.cpp | 1 + tests/vehicle_power_test.cpp | 1 + tests/vision_test.cpp | 2 ++ 15 files changed, 34 insertions(+), 5 deletions(-) diff --git a/src/activity_handlers.cpp b/src/activity_handlers.cpp index 090f84b5220cf..37c86cb0b3e36 100644 --- a/src/activity_handlers.cpp +++ b/src/activity_handlers.cpp @@ -2004,6 +2004,7 @@ void activity_handlers::vehicle_finish( player_activity *act, player *p ) act->values.size() ); } else { if( vp ) { + g->m.invalidate_map_cache( g->get_levz() ); g->refresh_all(); // TODO: Z (and also where the activity is queued) // Or not, because the vehicle coordinates are dropped anyway @@ -2562,6 +2563,7 @@ void activity_handlers::meditate_finish( player_activity *act, player *p ) void activity_handlers::aim_do_turn( player_activity *act, player * ) { if( act->index == 0 ) { + g->m.invalidate_map_cache( g->get_levz() ); g->m.build_map_cache( g->get_levz() ); g->plfire(); } diff --git a/src/bionics.cpp b/src/bionics.cpp index ba876640e8729..c39587b6a7df1 100644 --- a/src/bionics.cpp +++ b/src/bionics.cpp @@ -1057,6 +1057,7 @@ bool player::uninstall_bionic( const bionic_id &b_id, player &installer, bool au } bionics_uninstall_failure( installer, difficulty, success, adjusted_skill ); } + g->m.invalidate_map_cache( g->get_levz() ); g->refresh_all(); return true; } @@ -1151,6 +1152,7 @@ bool player::install_bionics( const itype &type, player &installer, bool autodoc } bionics_install_failure( installer, difficult, success, adjusted_skill ); } + g->m.invalidate_map_cache( g->get_levz() ); g->refresh_all(); return true; } diff --git a/src/game.cpp b/src/game.cpp index d08a57a8aed3c..1e6464ef93cb2 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -728,10 +728,12 @@ bool game::start_game() lev.y -= HALF_MAPSIZE; load_map( lev ); + m.invalidate_map_cache( get_levz() ); m.build_map_cache( get_levz() ); // Do this after the map cache has been built! start_loc.place_player( u ); // ...but then rebuild it, because we want visibility cache to avoid spawning monsters in sight + m.invalidate_map_cache( get_levz() ); m.build_map_cache( get_levz() ); // Start the overmap with out immediate neighborhood visible, this needs to be after place_player overmap_buffer.reveal( point( u.global_omt_location().x, u.global_omt_location().y ), @@ -1495,6 +1497,7 @@ 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 ) ) { @@ -1957,7 +1960,6 @@ void game::handle_key_blocking_activity() cancel_activity_query( _( "Confirm:" ) ); } else if( action == "player_data" ) { u.disp_info(); - refresh_all(); } else if( action == "messages" ) { Messages::display_messages(); refresh_all(); @@ -3393,6 +3395,7 @@ void game::debug() break; } catacurses::erase(); + m.invalidate_map_cache( g->get_levz() ); refresh_all(); } @@ -3635,6 +3638,7 @@ 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 ); @@ -7379,6 +7383,7 @@ look_around_result game::look_around( catacurses::window w_info, tripoint ¢e add_msg( m_debug, "levx: %d, levy: %d, levz :%d", get_levx(), get_levy(), center.z ); u.view_offset.z = center.z - u.posz(); + m.invalidate_map_cache( center.z ); refresh_all(); if( select_zone && has_first_point ) { // is blinking blink = true; // Always draw blink symbols when moving cursor @@ -7492,6 +7497,7 @@ look_around_result game::look_around( catacurses::window w_info, tripoint ¢e action != "throw_blind" ); if( m.has_zlevels() && center.z != old_levz ) { + m.invalidate_map_cache( old_levz ); m.build_map_cache( old_levz ); u.view_offset.z = 0; } @@ -11776,6 +11782,7 @@ void game::vertical_move( int movez, bool force ) } } + m.invalidate_map_cache( g->get_levz() ); refresh_all(); // Upon force movement, traps can not be avoided. m.creature_on_trap( u, !force ); @@ -12052,6 +12059,7 @@ point game::update_map( int &x, int &y ) load_npcs(); // Make sure map cache is consistent since it may have shifted. + m.invalidate_map_cache( get_levz() ); m.build_map_cache( get_levz() ); // Spawn monsters if appropriate diff --git a/src/handle_action.cpp b/src/handle_action.cpp index 90fa95cc7dea0..d24243a856a56 100644 --- a/src/handle_action.cpp +++ b/src/handle_action.cpp @@ -1837,6 +1837,7 @@ bool game::handle_action() add_msg( m_info, _( "You can't disassemble items while driving." ) ); } else { u.disassemble(); + g->m.invalidate_map_cache( g->get_levz() ); refresh_all(); } break; @@ -1951,7 +1952,6 @@ bool game::handle_action() case ACTION_PL_INFO: u.disp_info(); - refresh_all(); break; case ACTION_MAP: @@ -1978,7 +1978,6 @@ bool game::handle_action() case ACTION_FACTIONS: new_faction_manager_ptr->display(); - refresh_all(); break; case ACTION_MORALE: @@ -2003,7 +2002,6 @@ bool game::handle_action() case ACTION_OPTIONS: get_options().show( true ); - refresh_all(); g->init_ui( true ); break; @@ -2032,7 +2030,6 @@ bool game::handle_action() break; //don't do anything when sharing and not debugger } debug(); - refresh_all(); break; case ACTION_TOGGLE_FULLSCREEN: diff --git a/src/map.h b/src/map.h index cc8adc0403d22..e5526120a34c7 100644 --- a/src/map.h +++ b/src/map.h @@ -264,6 +264,15 @@ class map } } + inline void invalidate_map_cache( const int zlev ) { + if( inbounds_z( zlev ) ) { + level_cache &ch = get_cache( zlev ); + ch.floor_cache_dirty = true; + ch.transparency_cache_dirty = true; + ch.outside_cache_dirty = true; + } + } + bool check_and_set_seen_cache( const tripoint &p ) const { std::bitset &memory_seen_cache = get_cache( p.z ).map_memory_seen_cache; diff --git a/src/mapgen.cpp b/src/mapgen.cpp index 21f57fac54d38..9ed4c7c9ae416 100644 --- a/src/mapgen.cpp +++ b/src/mapgen.cpp @@ -8615,6 +8615,7 @@ bool update_mapgen_function_json::update_map( const tripoint &omt_pos, int offse update_map.save(); g->load_npcs(); + g->m.invalidate_map_cache( omt_pos.z ); g->refresh_all(); return true; } diff --git a/src/ranged.cpp b/src/ranged.cpp index 12952d907043c..2265b0f715a93 100644 --- a/src/ranged.cpp +++ b/src/ranged.cpp @@ -1422,6 +1422,7 @@ std::vector target_handler::target_ui( player &pc, target_mode mode, // We need to do a bunch of redrawing and cache updates since we're // looking at a different z-level. + g->m.invalidate_map_cache( dst.z ); g->refresh_all(); } diff --git a/src/start_location.cpp b/src/start_location.cpp index 2d920c551001d..13d39a7b86cf1 100644 --- a/src/start_location.cpp +++ b/src/start_location.cpp @@ -310,6 +310,7 @@ void start_location::place_player( player &u ) const u.setx( HALF_MAPSIZE_X ); u.sety( HALF_MAPSIZE_Y ); u.setz( g->get_levz() ); + m.invalidate_map_cache( m.get_abs_sub().z ); m.build_map_cache( m.get_abs_sub().z ); const bool must_be_inside = flags().count( "ALLOW_OUTSIDE" ) == 0; ///\EFFECT_STR allows player to start behind less-bashable furniture and terrain diff --git a/src/vehicle_use.cpp b/src/vehicle_use.cpp index 88587735fe5a8..72f83be79493f 100644 --- a/src/vehicle_use.cpp +++ b/src/vehicle_use.cpp @@ -1365,6 +1365,7 @@ void vehicle::use_bike_rack( int part ) success = try_to_rack_nearby_vehicle( racks_parts ); } if( success ) { + g->m.invalidate_map_cache( g->get_levz() ); g->refresh_all(); } } diff --git a/tests/crafting_test.cpp b/tests/crafting_test.cpp index 935692d3b032b..512924c4a1641 100644 --- a/tests/crafting_test.cpp +++ b/tests/crafting_test.cpp @@ -413,6 +413,7 @@ static void set_time( int time ) g->reset_light_level(); int z = g->u.posz(); g->m.update_visibility_cache( z ); + g->m.invalidate_map_cache( z ); g->m.build_map_cache( z ); } diff --git a/tests/map_helpers.cpp b/tests/map_helpers.cpp index 0f63be073e81d..130a938de6e54 100644 --- a/tests/map_helpers.cpp +++ b/tests/map_helpers.cpp @@ -28,6 +28,7 @@ void wipe_map_terrain() for( wrapped_vehicle &veh : g->m.get_vehicles() ) { g->m.destroy_vehicle( veh.v ); } + g->m.invalidate_map_cache( 0 ); g->m.build_map_cache( 0, true ); } diff --git a/tests/vehicle_drag.cpp b/tests/vehicle_drag.cpp index fcb821c6b3b0b..b3fe4d406787e 100644 --- a/tests/vehicle_drag.cpp +++ b/tests/vehicle_drag.cpp @@ -56,6 +56,7 @@ void clear_game_drag( const ter_id &terrain ) g->m.destroy_vehicle( veh.v ); } + g->m.invalidate_map_cache( 0 ); g->m.build_map_cache( 0, true ); // hard force a rebuild of caches g->m.shift( 0, 1 ); diff --git a/tests/vehicle_efficiency.cpp b/tests/vehicle_efficiency.cpp index 1c8e6e03c7103..bc190553b5a3d 100644 --- a/tests/vehicle_efficiency.cpp +++ b/tests/vehicle_efficiency.cpp @@ -65,6 +65,7 @@ void clear_game( const ter_id &terrain ) g->m.destroy_vehicle( veh.v ); } + g->m.invalidate_map_cache( 0 ); g->m.build_map_cache( 0, true ); } diff --git a/tests/vehicle_power_test.cpp b/tests/vehicle_power_test.cpp index 121b7f2f82a6a..b81d845077176 100644 --- a/tests/vehicle_power_test.cpp +++ b/tests/vehicle_power_test.cpp @@ -28,6 +28,7 @@ TEST_CASE( "vehicle_power" ) g->m.i_clear( p ); } + g->m.invalidate_map_cache( 0 ); g->m.build_map_cache( 0, true ); const tripoint test_origin( 15, 15, 0 ); diff --git a/tests/vision_test.cpp b/tests/vision_test.cpp index 186d65278a470..bba99bb3923c5 100644 --- a/tests/vision_test.cpp +++ b/tests/vision_test.cpp @@ -120,8 +120,10 @@ void full_map_test( const std::vector &setup, // they might, for example, have poor nightvision due to having just been // in daylight) g->m.update_visibility_cache( origin.z ); + g->m.invalidate_map_cache( origin.z ); g->m.build_map_cache( origin.z ); g->m.update_visibility_cache( origin.z ); + g->m.invalidate_map_cache( origin.z ); g->m.build_map_cache( origin.z ); const level_cache &cache = g->m.access_cache( origin.z );