Skip to content

Commit

Permalink
improve npc overmap travel: (#45896)
Browse files Browse the repository at this point in the history
* recalculate goal when it's not reachable
* still look for needs if CITY_SIZE=0
* change `overmap_location::terrains` type to `flat_set`, refactor `npc::set_omt_destination`
  • Loading branch information
Aivean authored Dec 8, 2020
1 parent 357045b commit 949ecdf
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 30 deletions.
12 changes: 11 additions & 1 deletion src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4661,6 +4661,7 @@ void game::overmap_npc_move()
travelling_npcs.push_back( npc_to_add );
}
}
bool npcs_need_reload = false;
for( auto &elem : travelling_npcs ) {
if( elem->has_omt_destination() ) {
if( !elem->omt_path.empty() && rl_dist( elem->omt_path.back(), elem->global_omt_location() ) > 2 ) {
Expand All @@ -4669,17 +4670,26 @@ void game::overmap_npc_move()
}
if( elem->omt_path.empty() ) {
elem->omt_path = overmap_buffer.get_npc_path( elem->global_omt_location(), elem->goal );
if( elem->omt_path.empty() ) { // goal is unreachable, reset it
elem->goal = npc::no_goal_point;
}
} else {
if( elem->omt_path.back() == elem->global_omt_location() ) {
elem->omt_path.pop_back();
}
// TODO: fix point types
elem->travel_overmap(
project_to<coords::sm>( elem->omt_path.back() ).raw() );
npcs_need_reload = true;
}
reload_npcs();
} else if( calendar::once_every( 1_hours ) && one_in( 3 ) ) {
// travelling destination is reached/not set, try different one
elem -> set_omt_destination();
}
}
if( npcs_need_reload ) {
reload_npcs();
}
}

/* Knockback target at t by force number of tiles in direction from s to t
Expand Down
30 changes: 15 additions & 15 deletions src/npcmove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4084,40 +4084,40 @@ void npc::set_omt_destination()
// We need that, otherwise find_closest won't work properly
surface_omt_loc.z() = 0;

// also, don't bother looking if the CITY_SIZE is 0, just go somewhere at random
const int city_size = get_option<int>( "CITY_SIZE" );
if( city_size == 0 ) {
goal = surface_omt_loc + point( rng( -90, 90 ), rng( -90, 90 ) );
return;
}

decide_needs();
if( needs.empty() ) { // We don't need anything in particular.
needs.push_back( need_none );

// also, don't bother looking if the CITY_SIZE is 0, just go somewhere at random
const int city_size = get_option<int>( "CITY_SIZE" );
if( city_size == 0 ) {
goal = surface_omt_loc + point( rng( -90, 90 ), rng( -90, 90 ) );
return;
}
}

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 ) {
for( const oter_type_str_id &elem : get_location_for( fulfill )->get_all_terrains() ) {
std::pair<std::string, ot_match_type> temp_pair;
temp_pair.first = elem.id().str();
temp_pair.first = elem.str();
temp_pair.second = ot_match_type::type;
temp_types.push_back( temp_pair );
find_params.types.push_back( temp_pair );
}
// 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.types = temp_types;
find_params.existing_only = false;
goal = overmap_buffer.find_closest( surface_omt_loc, find_params );
omt_path = overmap_buffer.get_npc_path( surface_omt_loc, goal );
omt_path.clear();
if( goal != overmap::invalid_tripoint ) {
omt_path = overmap_buffer.get_npc_path( surface_omt_loc, goal );
}
if( !omt_path.empty() ) {
dest_type = overmap_buffer.ter( goal )->get_type_id().str();
break;
}
}
Expand Down
15 changes: 4 additions & 11 deletions src/overmap_location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ const overmap_location &string_id<overmap_location>::obj() const

bool overmap_location::test( const int_id<oter_t> &oter ) const
{
return std::any_of( terrains.cbegin(), terrains.cend(),
[ &oter ]( const oter_type_str_id & type ) {
return oter->type_is( type );
} );
return terrains.count( oter->get_type_id() );
}

oter_type_id overmap_location::get_random_terrain() const
Expand All @@ -53,13 +50,9 @@ void overmap_location::load( const JsonObject &jo, const std::string & )
}
}

std::vector<oter_type_id> overmap_location::get_all_terrains() const
const cata::flat_set<oter_type_str_id> &overmap_location::get_all_terrains() const
{
std::vector<oter_type_id> ret;
for( const oter_type_str_id &elem : terrains ) {
ret.push_back( elem );
}
return ret;
return terrains;
}

void overmap_location::check() const
Expand All @@ -81,7 +74,7 @@ void overmap_location::finalize()
oter_flags check_flag = it->second;
for( const oter_t &ter_elem : overmap_terrains::get_all() ) {
if( ter_elem.has_flag( check_flag ) ) {
terrains.push_back( ter_elem.get_type_id() );
terrains.insert( ter_elem.get_type_id() );
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/overmap_location.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <string>
#include <vector>

#include "flat_set.h"
#include "int_id.h"
#include "string_id.h"
#include "type_id.h"
Expand All @@ -15,22 +16,23 @@ struct oter_t;

struct overmap_location {
public:
using TerrColType = cata::flat_set<oter_type_str_id>;

void load( const JsonObject &jo, const std::string &src );
void check() const;
void finalize();

// Test if oter meets the terrain restrictions.
bool test( const int_id<oter_t> &oter ) const;
std::vector<oter_type_id> get_all_terrains() const;
const TerrColType &get_all_terrains() const;
oter_type_id get_random_terrain() const;

public:
// Used by generic_factory
string_id<overmap_location> id;
bool was_loaded = false;

private:
std::vector<oter_type_str_id> terrains;
TerrColType terrains;
std::vector<std::string> flags;
};

Expand Down

0 comments on commit 949ecdf

Please sign in to comment.