Skip to content

Commit

Permalink
perf(port): cache NPC goal (#4225)
Browse files Browse the repository at this point in the history
* Update npc.h

Co-authored-by: David Seguin <davidseguin@live.ca>

* Update npcmove.cpp

Co-authored-by: David Seguin <davidseguin@live.ca>

* fix: add missing comments, use emplace_back

---------

Co-authored-by: David Seguin <davidseguin@live.ca>
Co-authored-by: scarf <greenscarf005@gmail.com>
  • Loading branch information
3 people authored Feb 14, 2024
1 parent 68a05e9 commit 8946731
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 18 deletions.
7 changes: 7 additions & 0 deletions src/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,11 @@ struct npc_short_term_cache {
lru_cache<tripoint, int> searched_tiles;
};

struct npc_need_goal_cache {
tripoint_abs_omt goal;
tripoint_abs_omt omt_loc;
};

// DO NOT USE! This is old, use strings as talk topic instead, e.g. "TALK_AGREE_FOLLOW" instead of
// TALK_AGREE_FOLLOW. There is also convert_talk_topic which can convert the enumeration values to
// the new string values (used to load old saves).
Expand Down Expand Up @@ -1245,6 +1250,8 @@ class npc : public player
std::map<std::string, time_point> complaints;

npc_short_term_cache ai_cache;

std::map<npc_need, npc_need_goal_cache> goal_cache;
public:
/**
* Global position, expressed in map square coordinate system
Expand Down
37 changes: 19 additions & 18 deletions src/npcmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4260,24 +4260,25 @@ void npc::set_omt_destination()

std::string dest_type;
for( const auto &fulfill : needs ) {
// look for the closest occurence of any of that locations terrain types
std::vector<oter_type_id> loc_list = get_location_for( fulfill )->get_all_terrains();
std::shuffle( loc_list.begin(), loc_list.end(), rng_get_engine() );
omt_find_params find_params;
std::vector<std::pair<std::string, ot_match_type>> temp_types;
for( const oter_type_id &elem : loc_list ) {
std::pair<std::string, ot_match_type> temp_pair;
temp_pair.first = elem.id().str();
temp_pair.second = ot_match_type::type;
temp_types.push_back( temp_pair );
}
find_params.search_range = 75;
find_params.min_distance = 0;
find_params.must_see = false;
find_params.cant_see = false;
find_params.types = temp_types;
find_params.existing_only = false;
goal = overmap_buffer.find_closest( surface_omt_loc, find_params );
auto cache_iter = goal_cache.find( fulfill );
if( cache_iter != goal_cache.end() && cache_iter->second.omt_loc == surface_omt_loc ) {
goal = cache_iter->second.goal;
} else {
// look for the closest occurrence of any of that locations terrain types
omt_find_params find_params;
for( const oter_type_id &elem : get_location_for( fulfill )->get_all_terrains() ) {
find_params.types.emplace_back( elem.id().str(), ot_match_type::type );
}
// note: no shuffle of `find_params.types` is needed, because `find_closest`
// disregards `types` order anyway, and already returns random result among
// those having equal minimal distance
find_params.search_range = 75;
find_params.existing_only = false;
goal = overmap_buffer.find_closest( surface_omt_loc, find_params );
npc_need_goal_cache &cache = goal_cache[fulfill];
cache.goal = goal;
cache.omt_loc = surface_omt_loc;
}
omt_path.clear();
if( goal != overmap::invalid_tripoint ) {
omt_path = overmap_buffer.get_travel_path( surface_omt_loc, goal, overmap_path_params::for_npc() );
Expand Down

0 comments on commit 8946731

Please sign in to comment.