Skip to content

Commit

Permalink
Merge pull request #44968 from John-Candlebury/fix-road-infinite-loop
Browse files Browse the repository at this point in the history
Remove posible infinite loop in place_roads
  • Loading branch information
ZhilkinSerg authored Oct 20, 2020
2 parents e6cd482 + 8927eba commit ea096c2
Showing 1 changed file with 42 additions and 21 deletions.
63 changes: 42 additions & 21 deletions src/overmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2745,34 +2745,55 @@ void overmap::place_roads( const overmap *north, const overmap *east, const over
tripoint_om_omt tmp;
// Populate viable_roads with one point for each neighborless side.
// Make sure these points don't conflict with rivers.
// TODO: In theory this is a potential infinite loop...

std::array < int, OMAPX - 20 > omap_num;
for( int i = 0; i < 160; i++ ) {
omap_num[i] = i + 10;
}

if( north == nullptr ) {
do {
tmp = tripoint_om_omt( rng( 10, OMAPX - 11 ), 0, 0 );
} while( is_river( ter( tmp ) ) || is_river( ter( tmp + point_east ) ) ||
is_river( ter( tmp + point_west ) ) );
viable_roads.push_back( tmp );
std::shuffle( omap_num.begin(), omap_num.end(), rng_get_engine() );
for( const auto &i : omap_num ) {
tmp = tripoint_om_omt( i, 0, 0 );
if( !( is_river( ter( tmp ) ) || is_river( ter( tmp + point_east ) ) ||
is_river( ter( tmp + point_west ) ) ) ) {
viable_roads.push_back( tmp );
break;
}
}
}
if( east == nullptr ) {
do {
tmp = tripoint_om_omt( OMAPX - 1, rng( 10, OMAPY - 11 ), 0 );
} while( is_river( ter( tmp ) ) || is_river( ter( tmp + point_north ) ) ||
is_river( ter( tmp + point_south ) ) );
viable_roads.push_back( tmp );
std::shuffle( omap_num.begin(), omap_num.end(), rng_get_engine() );
for( const auto &i : omap_num ) {
tmp = tripoint_om_omt( OMAPX - 1, i, 0 );
if( !( is_river( ter( tmp ) ) || is_river( ter( tmp + point_north ) ) ||
is_river( ter( tmp + point_south ) ) ) ) {
viable_roads.push_back( tmp );
break;
}
}
}
if( south == nullptr ) {
do {
tmp = tripoint_om_omt( rng( 10, OMAPX - 11 ), OMAPY - 1, 0 );
} while( is_river( ter( tmp ) ) || is_river( ter( tmp + point_east ) ) ||
is_river( ter( tmp + point_west ) ) );
viable_roads.push_back( tmp );
std::shuffle( omap_num.begin(), omap_num.end(), rng_get_engine() );
for( const auto &i : omap_num ) {
tmp = tripoint_om_omt( i, OMAPY - 1, 0 );
if( !( is_river( ter( tmp ) ) || is_river( ter( tmp + point_east ) ) ||
is_river( ter( tmp + point_west ) ) ) ) {
viable_roads.push_back( tmp );
break;
}
}
}
if( west == nullptr ) {
do {
tmp = tripoint_om_omt( 0, rng( 10, OMAPY - 11 ), 0 );
} while( is_river( ter( tmp ) ) || is_river( ter( tmp + point_north ) ) ||
is_river( ter( tmp + point_south ) ) );
viable_roads.push_back( tmp );
std::shuffle( omap_num.begin(), omap_num.end(), rng_get_engine() );
for( const auto &i : omap_num ) {
tmp = tripoint_om_omt( 0, i, 0 );
if( !( is_river( ter( tmp ) ) || is_river( ter( tmp + point_north ) ) ||
is_river( ter( tmp + point_south ) ) ) ) {
viable_roads.push_back( tmp );
break;
}
}
}
while( roads_out.size() < 2 && !viable_roads.empty() ) {
roads_out.push_back( random_entry_removed( viable_roads ) );
Expand Down

0 comments on commit ea096c2

Please sign in to comment.