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

[Doesn't work, pls help] Json connection origins #67736

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
77 changes: 65 additions & 12 deletions src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1397,24 +1397,77 @@ struct fixed_overmap_special_data : overmap_special_data {
if( initial_dir != cube_direction::last ) {
initial_dir = initial_dir + dir;
}
if( cit ) {
om.build_connection( cit.pos, rp.xy(), elem.p.z, *elem.connection,
must_be_unexplored, initial_dir );
}
// if no city present, search for nearby road within 50 tiles and make
// connection to it instead
else {

const overmap_connection &connection = *elem.connection;
cata::flat_set<std::pair<std::pair<std::string, ot_match_type>, int>> origin_terrains = connection.origin_terrains;
Procyonae marked this conversation as resolved.
Show resolved Hide resolved
point_om_omt connection_point;
point_om_omt origin_connection_point;
point_om_omt city_connection_point;
int distance_to_origin = INT_MAX;
int distance_to_city = INT_MAX;

auto find_city = [&] () {
Procyonae marked this conversation as resolved.
Show resolved Hide resolved
if( connection.has_city_origin && cit ) {
distance_to_city = cit.get_distance_from( rp );
if ( distance_to_city <= connection.city_origin_max_distance ) {
Procyonae marked this conversation as resolved.
Show resolved Hide resolved
city_connection_point = cit.pos;
return true;
}
}
return false;
};

//Using this rather than a generic distance so it matches city's get_distance_from
auto get_distance_between = [&] ( tripoint_om_omt a, tripoint_om_omt b ) {
Procyonae marked this conversation as resolved.
Show resolved Hide resolved
return std::max( static_cast<int>( trig_dist( a, b ) ), 0 );
};

auto find_origin = [&] () {
Procyonae marked this conversation as resolved.
Show resolved Hide resolved
int max_range = 0;
for ( const std::pair<std::pair<std::string, ot_match_type>, int> &origin_terrain : origin_terrains ) {
Procyonae marked this conversation as resolved.
Show resolved Hide resolved
max_range = std::max( max_range, origin_terrain.second );
}
for( const tripoint_om_omt &nearby_point : closest_points_first( rp, max_range ) ) {
while( !origin_terrains.empty() ) {
for( const std::pair<std::pair<std::string, ot_match_type>, int> &origin_terrain : origin_terrains ) {
Procyonae marked this conversation as resolved.
Show resolved Hide resolved
const int distance = get_distance_between( rp, nearby_point );
if( origin_terrain.second < distance ) {
//Ideally remove it from origin_terrains
continue;
}
if( om.check_ot( origin_terrain.first.first, origin_terrain.first.second, nearby_point ) ) {
origin_connection_point = nearby_point.xy();
distance_to_origin = distance;
return true;
}
}
}
}
return false;
};

auto find_connection = [&] () {
Procyonae marked this conversation as resolved.
Show resolved Hide resolved
for( const tripoint_om_omt &nearby_point : closest_points_first( rp, 50 ) ) {
if( om.check_ot( "road", ot_match_type::contains, nearby_point ) ) {
om.build_connection(
nearby_point.xy(), rp.xy(), elem.p.z, *elem.connection,
must_be_unexplored, initial_dir );
if( connection.has( om.ter( nearby_point ) ) ) {
connection_point = nearby_point.xy();
return true;
}
}
return false;
};

Procyonae marked this conversation as resolved.
Show resolved Hide resolved
if( find_origin() || find_city() ) {
if( distance_to_origin < distance_to_city ) {
connection_point = origin_connection_point;
} else {
connection_point = city_connection_point;
}
} else if ( find_connection() ) {
Procyonae marked this conversation as resolved.
Show resolved Hide resolved
om.build_connection( connection_point, rp.xy(), elem.p.z, connection,
must_be_unexplored, initial_dir );
Procyonae marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

return result;
}

Expand Down
17 changes: 17 additions & 0 deletions src/overmap_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,23 @@ bool overmap_connection::has( const int_id<oter_t> &oter ) const
void overmap_connection::load( const JsonObject &jo, const std::string_view )
{
mandatory( jo, false, "subtypes", subtypes );

for( const JsonObject entry : jo.get_array( "origin" ) ) {

std::pair<std::string, ot_match_type> origin_terrain_partial;
std::pair<std::pair<std::string, ot_match_type>, int> origin_terrain;

Procyonae marked this conversation as resolved.
Show resolved Hide resolved
if( entry.has_member( "city" ) ) {
mandatory( entry, false, "city", has_city_origin );
optional( entry, false, "max_distance", city_origin_max_distance, 50 );
} else {
mandatory( entry, false, "om_terrain", origin_terrain_partial.first );
optional( entry, false, "om_terrain_match_type", origin_terrain_partial.second, ot_match_type::type );
Procyonae marked this conversation as resolved.
Show resolved Hide resolved
origin_terrain.first = origin_terrain_partial;
optional( entry, false, "max_distance", origin_terrain.second, 50 );
origin_terrains.insert( origin_terrain );
}
}
}

void overmap_connection::check() const
Expand Down
6 changes: 5 additions & 1 deletion src/overmap_connection.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class overmap_connection
public:
enum class flag : int { orthogonal };

string_id<oter_type_t> terrain;
string_id<oter_type_t> terrain; //TODO: Make this std::pair<std::string, ot_match_type>

int basic_cost = 0;

Expand Down Expand Up @@ -52,6 +52,10 @@ class overmap_connection
void check() const;
void finalize();

cata::flat_set<std::pair<std::pair<std::string, ot_match_type>, int>> origin_terrains;
bool has_city_origin = false;
int city_origin_max_distance;

string_id<overmap_connection> id;
std::vector<std::pair<string_id<overmap_connection>, mod_id>> src;
bool was_loaded = false;
Expand Down