Skip to content

Commit

Permalink
Try to pick a safe direction to cut down a tree (#2286)
Browse files Browse the repository at this point in the history
* safe-fell-tree: Try to pick a safe direction to cut down a tree

* safe-fell-tree: astyle
  • Loading branch information
OrenAudeles authored Feb 22, 2023
1 parent 1a50952 commit 59a7038
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/activity_handlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4184,21 +4184,41 @@ void activity_handlers::chop_tree_finish( player_activity *act, player *p )
}
}
} else {
// Try to safely fell tree
std::vector<tripoint> valid_directions;

for( const tripoint &elem : here.points_in_radius( pos, 1 ) ) {
bool cantuse = false;
tripoint direc = elem - pos;
tripoint proposed_to = pos + point( 3 * direction.x, 3 * direction.y );
std::vector<tripoint> rough_tree_line = line_to( pos, proposed_to );
for( const tripoint &elem : rough_tree_line ) {
// Try not to drop onto a critter
if( g->critter_at( elem ) ) {
cantuse = true;
break;
}

ter_t ter = here.ter( elem ).obj();
furn_t furn = here.furn( elem ).obj();
// Furniture / Terrain test
if( elem != pos && ( ter.bash.str_max != -1 || ( furn.id && furn.bash.str_max != -1 ) ) ) {
cantuse = true;
break;
}
// Vehicle check
if( veh_pointer_or_null( here.veh_at( elem ) ) ) {
cantuse = true;
break;
}
}
if( !cantuse ) {
direction = direc;
// Passed all tests for safe direction, add to the possible routes
valid_directions.push_back( direc );
}
}
// Select a random valid direction, or none if empty
direction = random_entry( valid_directions, direction );
}

const tripoint to = pos + 3 * direction.xy() + point( rng( -1, 1 ), rng( -1, 1 ) );
Expand Down

0 comments on commit 59a7038

Please sign in to comment.