Skip to content

Commit

Permalink
Merge pull request CleverRaven#73478 from PatrikLundell/fix_shift
Browse files Browse the repository at this point in the history
Changed map::shift to call add_roofs and compressed the code
  • Loading branch information
Maleclypse authored May 4, 2024
2 parents 71cd072 + 70631f7 commit 3a6c4bb
Showing 1 changed file with 38 additions and 72 deletions.
110 changes: 38 additions & 72 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7993,13 +7993,19 @@ shift_bitset_cache<MAPSIZE, 1>( std::bitset<MAPSIZE *MAPSIZE> &cache, const poin

void map::shift( const point &sp )
{
if( !zlevels ) {
debugmsg( "map::shift called from map that doesn't support Z levels" );
return;
}

// Special case of 0-shift; refresh the map
if( sp == point_zero ) {
return; // Skip this?
}

if( std::abs( sp.x ) > 1 || std::abs( sp.y ) > 1 ) {
debugmsg( "map::shift called with a shift of more than one submap" );
return;
}

const tripoint_abs_sm abs = get_abs_sub();
Expand All @@ -8014,8 +8020,9 @@ void map::shift( const point &sp )

vehicle *remoteveh = g->remoteveh();

const int zmin = zlevels ? -OVERMAP_DEPTH : abs.z();
const int zmax = zlevels ? OVERMAP_HEIGHT : abs.z();
const int zmin = -OVERMAP_DEPTH;
const int zmax = OVERMAP_HEIGHT;

for( int gridz = zmin; gridz <= zmax; gridz++ ) {
level_cache *cache = get_cache_lazy( gridz );
if( !cache ) {
Expand All @@ -8030,6 +8037,14 @@ void map::shift( const point &sp )
// sx and sy should never be bigger than +/-1.
// absx and absy are our position in the world, for saving/loading purposes.
clear_vehicle_level_caches();

const int x_start = sp.x >= 0 ? 0 : my_MAPSIZE - 1;
const int x_stop = sp.x >= 0 ? my_MAPSIZE : -1;
const int x_step = sp.x >= 0 ? 1 : -1;
const int y_start = sp.y >= 0 ? 0 : my_MAPSIZE - 1;
const int y_stop = sp.y >= 0 ? my_MAPSIZE : -1;
const int y_step = sp.y >= 0 ? 1 : -1;

for( int gridz = zmin; gridz <= zmax; gridz++ ) {
// Clear vehicle list and rebuild after shift
// mlangsdorf 2020 - this is kind of insane, building the cache is not free, why are
Expand All @@ -8041,81 +8056,32 @@ void map::shift( const point &sp )
shift_bitset_cache<MAPSIZE_X, SEEX>( cache->map_memory_cache_ter, sp );
shift_bitset_cache<MAPSIZE, 1>( cache->field_cache, sp );
}
if( sp.x >= 0 ) {
for( int gridx = 0; gridx < my_MAPSIZE; gridx++ ) {
if( sp.y >= 0 ) {
for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) {
const tripoint grid( gridx, gridy, gridz );
if( gridx + sp.x < my_MAPSIZE && gridy + sp.y < my_MAPSIZE ) {
copy_grid( grid, grid + sp );
submap *const cur_submap = get_submap_at_grid( grid );
if( cur_submap == nullptr ) {
debugmsg( "Tried to update vehicle list at %s but the submap is not loaded", grid.to_string() );
continue;
}
update_vehicle_list( cur_submap, gridz );
} else {
loadn( grid, true );
loaded_grids.emplace_back( grid );
}
}
} else { // sy < 0; work through it backwards
for( int gridy = my_MAPSIZE - 1; gridy >= 0; gridy-- ) {
const tripoint grid( gridx, gridy, gridz );
if( gridx + sp.x < my_MAPSIZE && gridy + sp.y >= 0 ) {
copy_grid( grid, grid + sp );
submap *const cur_submap = get_submap_at_grid( grid );
if( cur_submap == nullptr ) {
debugmsg( "Tried to update vehicle list at %s but the submap is not loaded", grid.to_string() );
continue;
}
update_vehicle_list( cur_submap, gridz );
} else {
loadn( grid, true );
loaded_grids.emplace_back( grid );
}
}

for( int gridx = x_start; gridx != x_stop; gridx += x_step ) {
for( int gridy = y_start; gridy != y_stop; gridy += y_step ) {
if( gridx + sp.x != x_stop && gridy + sp.y != y_stop ) {
for( int gridz = zmin; gridz <= zmax; gridz++ ) {
const tripoint grid( gridx, gridy, gridz );

copy_grid( grid, grid + sp );
submap *const cur_submap = get_submap_at_grid( grid );
if( cur_submap == nullptr ) {
debugmsg( "Tried to update vehicle list at %s but the submap is not loaded", grid.to_string() );
continue;
}
update_vehicle_list( cur_submap, gridz );
}
}
} else { // sx < 0; work through it backwards
for( int gridx = my_MAPSIZE - 1; gridx >= 0; gridx-- ) {
if( sp.y >= 0 ) {
for( int gridy = 0; gridy < my_MAPSIZE; gridy++ ) {
const tripoint grid( gridx, gridy, gridz );
if( gridx + sp.x >= 0 && gridy + sp.y < my_MAPSIZE ) {
copy_grid( grid, grid + sp );
submap *const cur_submap = get_submap_at_grid( grid );
if( cur_submap == nullptr ) {
debugmsg( "Tried to update vehicle list at %s but the submap is not loaded", grid.to_string() );
continue;
}
update_vehicle_list( cur_submap, gridz );
} else {
loadn( grid, true );
loaded_grids.emplace_back( grid );
}
}
} else { // sy < 0; work through it backwards
for( int gridy = my_MAPSIZE - 1; gridy >= 0; gridy-- ) {
const tripoint grid( gridx, gridy, gridz );
if( gridx + sp.x >= 0 && gridy + sp.y >= 0 ) {
copy_grid( grid, grid + sp );
submap *const cur_submap = get_submap_at_grid( grid );
if( cur_submap == nullptr ) {
debugmsg( "Tried to update vehicle list at %s but the submap is not loaded", grid.to_string() );
continue;
}
update_vehicle_list( cur_submap, gridz );
} else {
loadn( grid, true );
loaded_grids.emplace_back( grid );
}
}
} else {
loadn( point( gridx, gridy ), true );

for( int gridz = zmin; gridz <= zmax; gridz++ ) {
loaded_grids.emplace_back( gridx, gridy, gridz );
}
}
}

}

rebuild_vehicle_level_caches();

g->setremoteveh( remoteveh );
Expand Down Expand Up @@ -8746,7 +8712,7 @@ void map::add_roofs( const tripoint &grid )
for( int x = 0; x < SEEX; x++ ) {
for( int y = 0; y < SEEY; y++ ) {
const ter_id ter_here = sub_here->get_ter( { x, y } );
if( !ter_here->has_flag( "EMPTY_SPACE" ) ) {
if( ter_here.id() != ter_t_open_air ) {
continue;
}

Expand Down

0 comments on commit 3a6c4bb

Please sign in to comment.