diff --git a/src/cata_tiles.cpp b/src/cata_tiles.cpp index 42af38a1c884f..acca8f41bf46e 100644 --- a/src/cata_tiles.cpp +++ b/src/cata_tiles.cpp @@ -2727,8 +2727,8 @@ void cata_tiles::do_tile_loading_report() { //TODO: exclude fake items from Item_factory::init_old() tile_loading_report(item_controller->get_all_itypes(), "Items", ""); auto mtypes = MonsterGenerator::generator().get_all_mtypes(); - lr_generic( mtypes.begin(), mtypes.end(), []( std::vector::iterator m ) { - return ( *m )->id.str(); + lr_generic( mtypes.begin(), mtypes.end(), []( const std::vector::iterator &m ) { + return ( *m ).id.str(); }, "Monsters", "" ); tile_loading_report(vpart_info::get_all().size(), "Vehicle Parts", "vp_"); tile_loading_report(trap::count(), "Traps", ""); diff --git a/src/defense.cpp b/src/defense.cpp index 006343806274d..afd0846767142 100644 --- a/src/defense.cpp +++ b/src/defense.cpp @@ -179,7 +179,7 @@ void defense_game::game_over() void defense_game::init_mtypes() { for( auto &type : MonsterGenerator::generator().get_all_mtypes() ) { - mtype *const t = const_cast( type ); + mtype *const t = const_cast( &type ); t->difficulty *= 1.5; t->difficulty += int( t->difficulty / 5 ); t->flags.insert( MF_BASHES ); diff --git a/src/game.cpp b/src/game.cpp index caedae926b185..e74cfac6b7b09 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1111,8 +1111,8 @@ bool game::cleanup_at_end() int iTotalKills = 0; for( const auto &type : MonsterGenerator::generator().get_all_mtypes() ) { - if( kill_count( type->id ) > 0 ) { - iTotalKills += kill_count( type->id ); + if( kill_count( type.id ) > 0 ) { + iTotalKills += kill_count( type.id ); } } diff --git a/src/generic_factory.h b/src/generic_factory.h index 88de640201b79..5ba9496f8c614 100644 --- a/src/generic_factory.h +++ b/src/generic_factory.h @@ -198,22 +198,14 @@ class generic_factory /** * Returns all the loaded objects. It can be used to iterate over them. * This returns a reference and is therefor quite fast, but you can also - * use @ref get_all, which returns a copy of this data with raw pointers. - * You should prefer `get_all` as it exposes a more stable interface. + * use @ref get_all. You should prefer @ref get_all as it exposes a more + * stable interface. */ const std::unordered_map, T> &all_ref() const { return data; } - std::vector get_all() const { - std::vector result; - result.reserve( data.size() ); - - using pair = typename std::unordered_map, T>::value_type; - std::transform( data.begin(), data.end(), back_inserter( result ), []( const pair & p ) { - return &p.second; - } ); - - return result; + const std::vector &get_all() const { + return list; } /** * @name `string_id` interface functions diff --git a/src/monstergenerator.cpp b/src/monstergenerator.cpp index 0565fcc3d856c..d6275e8cfe9f3 100644 --- a/src/monstergenerator.cpp +++ b/src/monstergenerator.cpp @@ -576,7 +576,7 @@ void species_type::load( JsonObject &jo ) optional( jo, was_loaded, "fear_triggers", fear_trig, trigger_reader ); } -std::vector MonsterGenerator::get_all_mtypes() const +const std::vector &MonsterGenerator::get_all_mtypes() const { return mon_templates->get_all(); } diff --git a/src/monstergenerator.h b/src/monstergenerator.h index eb56caddb790d..3c53c903182b9 100644 --- a/src/monstergenerator.h +++ b/src/monstergenerator.h @@ -62,7 +62,7 @@ class MonsterGenerator void check_monster_definitions() const; - std::vector get_all_mtypes() const; + const std::vector &get_all_mtypes() const; mtype_id get_valid_hallucination() const; friend struct mtype; friend struct species_type; diff --git a/src/newcharacter.cpp b/src/newcharacter.cpp index d242c3bc4c770..fd9f9eb4f326e 100644 --- a/src/newcharacter.cpp +++ b/src/newcharacter.cpp @@ -243,9 +243,9 @@ void player::randomize( const bool random_scenario, points_left &points ) } if( random_scenario ) { std::vector scenarios; - for( const scenario *const scenptr : scenario::get_all() ) { - if (!scenptr->has_flag("CHALLENGE")) { - scenarios.emplace_back( scenptr ); + for( const auto &scen : scenario::get_all() ) { + if (!scen.has_flag("CHALLENGE")) { + scenarios.emplace_back( &scen ); } } g->scen = random_entry( scenarios ); @@ -342,7 +342,7 @@ void player::randomize( const bool random_scenario, points_left &points ) case 2: case 3: case 4: - if( allow_traits ) { + if( allow_traits ) { rn = random_good_trait(); auto &mdata = mutation_branch::get( rn ); if( !has_trait(rn) && points.trait_points_left() >= mdata.points && @@ -1307,13 +1307,13 @@ tab_direction set_profession(WINDOW *w, player *u, points_left &points) do { if (recalc_profs) { sorted_profs.clear(); - for( const profession *const profptr : profession::get_all() ) { - if ((g->scen->profsize() == 0 && profptr->has_flag("SCEN_ONLY") == false) || - g->scen->profquery( profptr->ident() ) ) { - if (!lcmatch(profptr->gender_appropriate_name(u->male), filterstring)) { + for( const auto &prof : profession::get_all() ) { + if ((g->scen->profsize() == 0 && prof.has_flag("SCEN_ONLY") == false) || + g->scen->profquery( prof.ident() ) ) { + if (!lcmatch(prof.gender_appropriate_name(u->male), filterstring)) { continue; } - sorted_profs.push_back(profptr); + sorted_profs.push_back(&prof); } } profs_length = sorted_profs.size(); @@ -1595,8 +1595,8 @@ tab_direction set_skills(WINDOW *w, player *u, points_left &points) std::map prof_skills; const auto &pskills = u->prof->skills(); - - std::copy( pskills.begin(), pskills.end(), + + std::copy( pskills.begin(), pskills.end(), std::inserter( prof_skills, prof_skills.begin() ) ); do { @@ -1843,11 +1843,11 @@ tab_direction set_scenario(WINDOW *w, player *u, points_left &points) do { if (recalc_scens) { sorted_scens.clear(); - for( const scenario *const scenptr : scenario::get_all() ) { - if (!lcmatch(scenptr->gender_appropriate_name(u->male), filterstring)) { + for( const auto &scen : scenario::get_all() ) { + if (!lcmatch(scen.gender_appropriate_name(u->male), filterstring)) { continue; } - sorted_scens.push_back( scenptr ); + sorted_scens.push_back( &scen ); } scens_length = sorted_scens.size(); if (scens_length == 0) { @@ -2128,10 +2128,10 @@ tab_direction set_description(WINDOW *w, player *u, const bool allow_reroll, poi uimenu select_location; select_location.text = _("Select a starting location."); int offset = 0; - for( const start_location *const loc : start_location::get_all() ) { - if (g->scen->allowed_start(loc->ident()) || g->scen->has_flag("ALL_STARTS")) { - select_location.entries.push_back( uimenu_entry( loc->name() ) ); - if( loc->ident() == u->start_location ) { + for( const auto &loc : start_location::get_all() ) { + if (g->scen->allowed_start(loc.ident()) || g->scen->has_flag("ALL_STARTS")) { + select_location.entries.push_back( uimenu_entry( loc.name() ) ); + if( loc.ident() == u->start_location ) { select_location.selected = offset; } offset++; @@ -2344,9 +2344,9 @@ tab_direction set_description(WINDOW *w, player *u, const bool allow_reroll, poi } else if ( action == "CHOOSE_LOCATION" ) { select_location.redraw(); select_location.query(); - for( const start_location *const loc : start_location::get_all() ) { - if( loc->name() == select_location.entries[ select_location.selected ].txt ) { - u->start_location = loc->ident(); + for( const auto &loc : start_location::get_all() ) { + if( loc.name() == select_location.entries[ select_location.selected ].txt ) { + u->start_location = loc.ident(); } } werase(select_location.window); diff --git a/src/player.cpp b/src/player.cpp index 03d2809ad275a..6b235ca12c038 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -2038,12 +2038,12 @@ void player::memorial( std::ofstream &memorial_file, std::string epitaph ) // map to kill count for( const auto &type : MonsterGenerator::generator().get_all_mtypes() ) { - if( g->kill_count( type->id ) > 0 ) { + if( g->kill_count( type.id ) > 0 ) { kill_counts[std::tuple( - type->nname(), - type->sym - )] += g->kill_count( type->id ); - total_kills += g->kill_count( type->id ); + type.nname(), + type.sym + )] += g->kill_count( type.id ); + total_kills += g->kill_count( type.id ); } } @@ -9472,7 +9472,7 @@ bool player::can_wear( const item& it, bool alert ) const if( ( ( it.covers( bp_foot_l ) && is_wearing_shoes( "left" ) ) || ( it.covers( bp_foot_r ) && is_wearing_shoes( "right") ) ) && - ( !it.has_flag( "OVERSIZE" ) || !it.has_flag( "OUTER" ) ) && + ( !it.has_flag( "OVERSIZE" ) || !it.has_flag( "OUTER" ) ) && !it.has_flag( "SKINTIGHT" ) && !it.has_flag( "BELTED" ) ) { // Checks to see if the player is wearing shoes if( alert ) { diff --git a/src/profession.cpp b/src/profession.cpp index deaa18f93f9fb..ca05f3d48c217 100644 --- a/src/profession.cpp +++ b/src/profession.cpp @@ -171,7 +171,7 @@ const profession *profession::weighted_random() } } -std::vector profession::get_all() +const std::vector &profession::get_all() { return all_profs.get_all(); } diff --git a/src/profession.h b/src/profession.h index ce28db1423bdf..6e528d75cafeb 100644 --- a/src/profession.h +++ b/src/profession.h @@ -70,7 +70,7 @@ enum add_type : int; static const profession *generic(); // points to the generic, default profession // return a random profession, weighted for use w/ random character creation or npcs static const profession *weighted_random(); - static std::vector get_all(); + static const std::vector &get_all(); static bool has_initialized(); // clear profession map, every profession pointer becames invalid! diff --git a/src/scenario.cpp b/src/scenario.cpp index 212ff01e6fe10..e53da47243108 100644 --- a/src/scenario.cpp +++ b/src/scenario.cpp @@ -129,7 +129,7 @@ const scenario *scenario::weighted_random() } } -std::vector scenario::get_all() +const std::vector &scenario::get_all() { return all_scenarios.get_all(); } diff --git a/src/scenario.h b/src/scenario.h index 2c4bb58028b83..ec64a2d5978ef 100644 --- a/src/scenario.h +++ b/src/scenario.h @@ -60,7 +60,7 @@ class scenario static const scenario* generic(); // points to the generic, default profession // return a random scenario, weighted for use w/ random character creation static const scenario* weighted_random(); - static std::vector get_all(); + static const std::vector &get_all(); // clear scenario map, every scenario pointer becames invalid! static void reset(); diff --git a/src/start_location.cpp b/src/start_location.cpp index c2ba63079af63..3a56a9cfe1135 100644 --- a/src/start_location.cpp +++ b/src/start_location.cpp @@ -56,7 +56,7 @@ std::string start_location::target() const return _target; } -std::vector start_location::get_all() +const std::vector &start_location::get_all() { return all_starting_locations.get_all(); } diff --git a/src/start_location.h b/src/start_location.h index 679db18f5bdf0..e3eb5080bd105 100644 --- a/src/start_location.h +++ b/src/start_location.h @@ -30,7 +30,7 @@ class start_location static void load_location( JsonObject &jsonobj ); static void reset(); - static std::vector get_all(); + static const std::vector &get_all(); /** * Find a suitable start location on the overmap. diff --git a/src/wish.cpp b/src/wish.cpp index e94ef2bfda858..32899744b2666 100644 --- a/src/wish.cpp +++ b/src/wish.cpp @@ -368,12 +368,12 @@ void game::wishmonster( const tripoint &p ) int i = 0; for( const auto &montype : MonsterGenerator::generator().get_all_mtypes() ) { - wmenu.addentry( i, true, 0, "%s", montype->nname().c_str() ); - wmenu.entries[i].extratxt.txt = montype->sym; - wmenu.entries[i].extratxt.color = montype->color; + wmenu.addentry( i, true, 0, "%s", montype.nname().c_str() ); + wmenu.entries[i].extratxt.txt = montype.sym; + wmenu.entries[i].extratxt.color = montype.color; wmenu.entries[i].extratxt.left = 1; ++i; - mtypes.push_back( montype ); + mtypes.push_back( &montype ); } do {