Skip to content

Commit

Permalink
Overwrite tiles with same id (#51080)
Browse files Browse the repository at this point in the history
* Overwrite tiles with same id

* report duplicated id

* Don't delete comment

Co-authored-by: olanti-p <[email protected]>
Co-authored-by: Zhilkin Serg <[email protected]>
  • Loading branch information
3 people authored Sep 6, 2021
1 parent 8b9a116 commit 0ab1dd9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/cata_tiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ tileset::find_tile_type_by_season( const std::string &id, season_type season ) c

tile_type &tileset::create_tile_type( const std::string &id, tile_type &&new_tile_type )
{
auto inserted = tile_ids.insert( std::make_pair( id, new_tile_type ) ).first;
// Must overwrite existing tile
// TODO: c++17 - replace [] + find() with insert_or_assign()
tile_ids[id] = std::move( new_tile_type );
auto inserted = tile_ids.find( id );

const std::string &inserted_id = inserted->first;
tile_type &inserted_tile = inserted->second;

Expand Down Expand Up @@ -976,6 +980,9 @@ void tileset_loader::load_tilejson_from_file( const JsonObject &config )
*/
tile_type &tileset_loader::load_tile( const JsonObject &entry, const std::string &id )
{
if( ts.find_tile_type( id ) ) {
ts.duplicate_ids.insert( id );
}
tile_type curr_subtile;

load_tile_spritelists( entry, curr_subtile.fg, "fg" );
Expand Down Expand Up @@ -3734,6 +3741,23 @@ void cata_tiles::draw_footsteps_frame( const tripoint &center )
}
/* END OF ANIMATION FUNCTIONS */

void cata_tiles::tile_loading_report_dups()
{

std::vector<std::string> dups_list;
const std::unordered_set<std::string> &dups_set = tileset_ptr->get_duplicate_ids();
std::copy( dups_set.begin(), dups_set.end(), std::back_inserter( dups_list ) );
// NOLINTNEXTLINE(cata-use-localized-sorting)
std::sort( dups_list.begin(), dups_list.end() );

std::string res;
for( const std::string &s : dups_list ) {
res += s;
res += " ";
}
DebugLog( D_INFO, DC_ALL ) << "Have duplicates: " << res;
}

void cata_tiles::init_light()
{
g->reset_light_level();
Expand Down Expand Up @@ -3983,6 +4007,7 @@ void cata_tiles::do_tile_loading_report()
tile_loading_report( vpart_info::all(), C_VEHICLE_PART, "vp_" );
tile_loading_report<trap>( trap::count(), C_TRAP, "" );
tile_loading_report<field_type>( field_type::count(), C_FIELD, "" );
tile_loading_report_dups();

// needed until DebugLog ostream::flush bugfix lands
DebugLog( D_INFO, DC_ALL );
Expand Down
9 changes: 9 additions & 0 deletions src/cata_tiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class tileset
std::vector<texture> overexposed_tile_values;
std::vector<texture> memory_tile_values;

std::unordered_set<std::string> duplicate_ids;

std::unordered_map<std::string, tile_type> tile_ids;
// caches both "default" and "_season_XXX" tile variants (to reduce the number of lookups)
// either variant can be either a `nullptr` or a pointer/reference to the real value (stored inside `tile_ids`)
Expand Down Expand Up @@ -168,6 +170,10 @@ class tileset
return get_if_available( index, memory_tile_values );
}

const std::unordered_set<std::string> &get_duplicate_ids() const {
return duplicate_ids;
}

tile_type &create_tile_type( const std::string &id, tile_type &&new_tile_type );
const tile_type *find_tile_type( const std::string &id ) const;
/**
Expand Down Expand Up @@ -563,6 +569,9 @@ class cata_tiles
template<typename Iter, typename Func>
void lr_generic( Iter begin, Iter end, Func id_func, TILE_CATEGORY category,
const std::string &prefix );

void tile_loading_report_dups();

/** Lighting */
void init_light();

Expand Down

0 comments on commit 0ab1dd9

Please sign in to comment.