Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not rebuild map cache when it is not necessary #29747

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
Expand Down
2 changes: 2 additions & 0 deletions src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down
10 changes: 9 additions & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand Down Expand Up @@ -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 ) ) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -3393,6 +3395,7 @@ void game::debug()
break;
}
catacurses::erase();
m.invalidate_map_cache( g->get_levz() );
refresh_all();
}

Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -7379,6 +7383,7 @@ look_around_result game::look_around( catacurses::window w_info, tripoint &cente

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
Expand Down Expand Up @@ -7492,6 +7497,7 @@ look_around_result game::look_around( catacurses::window w_info, tripoint &cente
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;
}
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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
Expand Down
5 changes: 1 addition & 4 deletions src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1951,7 +1952,6 @@ bool game::handle_action()

case ACTION_PL_INFO:
u.disp_info();
refresh_all();
break;

case ACTION_MAP:
Expand All @@ -1978,7 +1978,6 @@ bool game::handle_action()

case ACTION_FACTIONS:
new_faction_manager_ptr->display();
refresh_all();
break;

case ACTION_MORALE:
Expand All @@ -2003,7 +2002,6 @@ bool game::handle_action()

case ACTION_OPTIONS:
get_options().show( true );
refresh_all();
g->init_ui( true );
break;

Expand Down Expand Up @@ -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:
Expand Down
9 changes: 9 additions & 0 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ class map
}
}

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<MAPSIZE_X *MAPSIZE_Y> &memory_seen_cache =
get_cache( p.z ).map_memory_seen_cache;
Expand Down
1 change: 1 addition & 0 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/ranged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1422,6 +1422,7 @@ std::vector<tripoint> 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();
}

Expand Down
1 change: 1 addition & 0 deletions src/start_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/vehicle_use.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down
1 change: 1 addition & 0 deletions tests/crafting_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
1 change: 1 addition & 0 deletions tests/map_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
1 change: 1 addition & 0 deletions tests/vehicle_drag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
1 change: 1 addition & 0 deletions tests/vehicle_efficiency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
1 change: 1 addition & 0 deletions tests/vehicle_power_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down
2 changes: 2 additions & 0 deletions tests/vision_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,10 @@ void full_map_test( const std::vector<std::string> &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 );
Expand Down