From 0ab1dd9ba8baa8c66d4f7351ea9c64f198479889 Mon Sep 17 00:00:00 2001 From: Fris0uman <41293484+Fris0uman@users.noreply.github.com> Date: Mon, 6 Sep 2021 11:43:37 +0200 Subject: [PATCH] Overwrite tiles with same id (#51080) * Overwrite tiles with same id * report duplicated id * Don't delete comment Co-authored-by: olanti-p Co-authored-by: Zhilkin Serg --- src/cata_tiles.cpp | 27 ++++++++++++++++++++++++++- src/cata_tiles.h | 9 +++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/cata_tiles.cpp b/src/cata_tiles.cpp index 5e14eff3c789a..ea7825fde8bf6 100644 --- a/src/cata_tiles.cpp +++ b/src/cata_tiles.cpp @@ -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; @@ -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" ); @@ -3734,6 +3741,23 @@ void cata_tiles::draw_footsteps_frame( const tripoint ¢er ) } /* END OF ANIMATION FUNCTIONS */ +void cata_tiles::tile_loading_report_dups() +{ + + std::vector dups_list; + const std::unordered_set &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(); @@ -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::count(), C_TRAP, "" ); tile_loading_report( field_type::count(), C_FIELD, "" ); + tile_loading_report_dups(); // needed until DebugLog ostream::flush bugfix lands DebugLog( D_INFO, DC_ALL ); diff --git a/src/cata_tiles.h b/src/cata_tiles.h index c736c3c76a5fa..45309a5499ee0 100644 --- a/src/cata_tiles.h +++ b/src/cata_tiles.h @@ -126,6 +126,8 @@ class tileset std::vector overexposed_tile_values; std::vector memory_tile_values; + std::unordered_set duplicate_ids; + std::unordered_map 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`) @@ -168,6 +170,10 @@ class tileset return get_if_available( index, memory_tile_values ); } + const std::unordered_set &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; /** @@ -563,6 +569,9 @@ class cata_tiles template 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();