Skip to content

Commit a401309

Browse files
committed
Dedupe follower list creation
1 parent 3aa1b84 commit a401309

File tree

6 files changed

+28
-40
lines changed

6 files changed

+28
-40
lines changed

src/faction.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
#include "localized_comparator.h"
2727
#include "map.h"
2828
#include "map_scale_constants.h"
29-
#include "memory_fast.h"
3029
#include "mission_companion.h"
3130
#include "mtype.h"
3231
#include "npc.h"
@@ -1068,16 +1067,7 @@ void faction_manager::display() const
10681067

10691068
avatar &player_character = get_avatar();
10701069
while( true ) {
1071-
// create a list of NPCs, visible and the ones on overmapbuffer
1072-
followers.clear();
1073-
for( const character_id &elem : g->get_follower_list() ) {
1074-
shared_ptr_fast<npc> npc_to_get = overmap_buffer.find_npc( elem );
1075-
if( !npc_to_get ) {
1076-
continue;
1077-
}
1078-
npc *npc_to_add = npc_to_get.get();
1079-
followers.push_back( npc_to_add );
1080-
}
1070+
overmap_buffer.populate_followers_vec( followers );
10811071
valfac.clear();
10821072
for( const auto &elem : g->faction_manager_ptr->all() ) {
10831073
if( elem.second.known_by_u && elem.second.id != faction_your_followers ) {

src/faction_camp.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2307,15 +2307,7 @@ void basecamp::worker_assignment_ui()
23072307

23082308
while( true ) {
23092309
// create a list of npcs stationed at this camp
2310-
followers.clear();
2311-
for( const character_id &elem : g->get_follower_list() ) {
2312-
shared_ptr_fast<npc> npc_to_get = overmap_buffer.find_npc( elem );
2313-
if( !npc_to_get || !npc_to_get->is_following() || npc_to_get->is_hallucination() ) {
2314-
continue;
2315-
}
2316-
npc *npc_to_add = npc_to_get.get();
2317-
followers.push_back( npc_to_add );
2318-
}
2310+
overmap_buffer.populate_followers_vec( followers, true, true );
23192311
cur_npc = nullptr;
23202312
if( !followers.empty() ) {
23212313
cur_npc = followers[selection];

src/npcmove.cpp

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3944,14 +3944,7 @@ bool npc::would_take_that( const item &it, const tripoint_bub_ms &p )
39443944
return false;
39453945
}
39463946
std::vector<npc *> followers;
3947-
for( const character_id &elem : g->get_follower_list() ) {
3948-
shared_ptr_fast<npc> npc_to_get = overmap_buffer.find_npc( elem );
3949-
if( !npc_to_get ) {
3950-
continue;
3951-
}
3952-
npc *npc_to_add = npc_to_get.get();
3953-
followers.push_back( npc_to_add );
3954-
}
3947+
overmap_buffer.populate_followers_vec( followers );
39553948
for( npc *&elem : followers ) {
39563949
if( elem->sees( here, this->pos_bub( here ) ) || elem->sees( here, p ) ) {
39573950
return false;

src/overmap_ui.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,12 @@
2323
#include "calendar.h"
2424
#include "cata_assert.h"
2525
#include "cata_variant.h"
26-
#include "character_id.h"
2726
#include "debug.h"
2827
#include "enum_conversions.h"
2928
#include "input_enums.h"
3029
#include "mapdata.h"
3130
#include "mapgen_parameter.h"
3231
#include "mapgendata.h"
33-
#include "memory_fast.h"
3432
#include "monster.h"
3533
#include "simple_pathfinding.h"
3634
#include "translation.h"
@@ -698,15 +696,7 @@ static void draw_ascii( const catacurses::window &w, overmap_draw_data_t &data )
698696
}
699697
}
700698
std::vector<npc *> followers;
701-
// get friendly followers
702-
for( const character_id &elem : g->get_follower_list() ) {
703-
shared_ptr_fast<npc> npc_to_get = overmap_buffer.find_npc( elem );
704-
if( !npc_to_get ) {
705-
continue;
706-
}
707-
npc *npc_to_add = npc_to_get.get();
708-
followers.push_back( npc_to_add );
709-
}
699+
overmap_buffer.populate_followers_vec( followers );
710700
if( !display_path.empty() ) {
711701
for( const tripoint_abs_omt &elem : display_path ) {
712702
npc_path_route.insert( elem );

src/overmapbuffer.cpp

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1420,6 +1420,22 @@ shared_ptr_fast<npc> overmapbuffer::find_npc( character_id id )
14201420
return nullptr;
14211421
}
14221422

1423+
void overmapbuffer::populate_followers_vec( std::vector<npc *> &followers,
1424+
bool only_following, bool ignore_hallu ) const
1425+
{
1426+
followers.clear();
1427+
for( const character_id &elem : g->get_follower_list() ) {
1428+
shared_ptr_fast<npc> npc_to_get = overmap_buffer.find_npc( elem );
1429+
if( !npc_to_get ||
1430+
( only_following && !npc_to_get->is_following() ) ||
1431+
( ignore_hallu && npc_to_get->is_hallucination() ) ) {
1432+
continue;
1433+
}
1434+
npc *npc_to_add = npc_to_get.get();
1435+
followers.push_back( npc_to_add );
1436+
}
1437+
}
1438+
14231439
void overmapbuffer::foreach_npc( const std::function<void( npc & )> &callback )
14241440
{
14251441
for( auto &it : overmaps ) {
@@ -1485,7 +1501,8 @@ std::vector<overmap *> overmapbuffer::get_overmaps_near( const tripoint_abs_sm &
14851501
return get_overmaps_near( location.xy(), radius );
14861502
}
14871503

1488-
std::vector<overmap *> overmapbuffer::get_overmaps_near( const point_abs_sm &p, const int radius )
1504+
std::vector<overmap *> overmapbuffer::get_overmaps_near( const point_abs_sm &p,
1505+
const int radius )
14891506
{
14901507
// Grab the corners of a square around the target location at distance radius.
14911508
// Convert to overmap coordinates and iterate from the minimum to the maximum.

src/overmapbuffer.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,12 @@ class overmapbuffer
318318
* Searches all loaded overmaps.
319319
*/
320320
shared_ptr_fast<npc> find_npc( character_id id );
321+
/**
322+
* Clear and fill a vector with NPCs who are your followers.
323+
* Optionally only include is_following() or exclude is_hallucination()
324+
*/
325+
void populate_followers_vec( std::vector<npc *> &followers, bool only_following = false,
326+
bool ignore_hallu = false ) const;
321327
void foreach_npc( const std::function<void( npc & )> &callback );
322328
shared_ptr_fast<npc> find_npc_by_unique_id( const std::string &unique_id );
323329
/**

0 commit comments

Comments
 (0)