Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(port): cache NPC goal #4225

Merged
merged 3 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading