Skip to content

Commit

Permalink
Added cleanup for unsaved submaps
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrikLundell committed Jun 6, 2024
1 parent 9f7e619 commit e6c5d77
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
7 changes: 2 additions & 5 deletions src/editmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2209,12 +2209,9 @@ void editmap::edit_mapgen()
/*
* Special voodoo sauce required to cleanse vehicles and caches to prevent debugmsg loops when re-applying mapgen.
*/
void editmap::cleartmpmap( tinymap &tmpmap ) const
void editmap::cleartmpmap( smallmap &tmpmap ) const
{
for( submap *&smap : tmpmap.grid ) {
delete smap;
smap = nullptr;
}
tmpmap.delete_unmerged_submaps();

for( int z = -OVERMAP_DEPTH; z <= OVERMAP_HEIGHT; z++ ) {
level_cache &ch = tmpmap.get_cache( z );
Expand Down
3 changes: 2 additions & 1 deletion src/editmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
class Creature;
class field;
class map;
class smallmap;
class tinymap;
class ui_adaptor;
class uilist;
Expand Down Expand Up @@ -60,7 +61,7 @@ class editmap
void edit_itm();
void edit_critter( Creature &critter );
void edit_mapgen();
void cleartmpmap( tinymap &tmpmap ) const;
void cleartmpmap( smallmap &tmpmap ) const;
void mapgen_preview( const real_coords &tc, uilist &gmenu );
vehicle *mapgen_veh_query( const tripoint_abs_omt &omt_tgt );
bool mapgen_veh_destroy( const tripoint_abs_omt &omt_tgt, vehicle *car_target );
Expand Down
4 changes: 4 additions & 0 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,9 @@ class map
// The code relies on the submap coordinate falling on omt boundaries, so taking a
// tripoint_abs_omt coordinate guarantees this will be fulfilled.
void generate( const tripoint_abs_omt &p, const time_point &when, bool save_results );
// Used when contents has been generated by 'generate' with save_results = false to dispose of
// submaps that aren't present in the map buffer. This is done to avoid memory leaks.
void delete_unmerged_submaps();
void place_spawns( const mongroup_id &group, int chance,
const point_bub_ms &p1, const point_bub_ms &p2, int z_level, float density,
bool individual = false, bool friendly = false,
Expand Down Expand Up @@ -2559,6 +2562,7 @@ class tinymap : private map
using map::is_main_cleanup_queued;
using map::main_cleanup_override;
using map::generate;
using map::delete_unmerged_submaps;
void place_spawns( const mongroup_id &group, int chance,
const point_omt_ms &p1, const point_omt_ms &p2, const int z_level, float density,
bool individual = false, bool friendly = false,
Expand Down
22 changes: 22 additions & 0 deletions src/mapgen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,28 @@ void map::generate( const tripoint_abs_omt &p, const time_point &when, bool save
set_abs_sub( p_sm_base );
}

void map::delete_unmerged_submaps()
{
tripoint_abs_sm sm_base = get_abs_sub();

for( size_t index = 0; index < grid.size(); index++ ) {
tripoint offset;
const int ix = static_cast<int>( index );

// This is the inverse of get_nonant.
if( zlevels ) {
offset = { ( ix / OVERMAP_LAYERS ) % my_MAPSIZE, ix / OVERMAP_LAYERS / my_MAPSIZE, ix % OVERMAP_LAYERS - OVERMAP_DEPTH };
} else {
offset = { ix % my_MAPSIZE, ix / my_MAPSIZE, sm_base.z()};
}

if( grid[index] != nullptr && MAPBUFFER.lookup_submap( sm_base.xy() + offset ) != grid[index] ) {
delete grid[index];
grid[index] = nullptr;
}
}
}

void mapgen_function_builtin::generate( mapgendata &mgd )
{
( *fptr )( mgd );
Expand Down
1 change: 1 addition & 0 deletions tests/overmap_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ TEST_CASE( "overmap_terrain_coverage", "[overmap][slow]" )
sample_size = goal_samples - p.second.samples;
p.second.found = true;
}
tm.delete_unmerged_submaps();
}
} );
p.second.samples = goal_samples;
Expand Down

0 comments on commit e6c5d77

Please sign in to comment.