From 443da66b6f28a24f4e551f05d3a8be6607b64ce8 Mon Sep 17 00:00:00 2001 From: davidpwbrown <39344466+davidpwbrown@users.noreply.github.com> Date: Sat, 4 Jan 2020 20:07:52 +0000 Subject: [PATCH] Fix garage door knockback crash (#36671) --- src/game.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index f0c46801f15ca..09de09b3bd7c5 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -5050,18 +5050,19 @@ bool game::forced_door_closing( const tripoint &p, const ter_id &door_type, int const std::string &door_name = door_type.obj().name(); int kbx = x; // Used when player/monsters are knocked back int kby = y; // and when moving items out of the way - for( int i = 0; i < 20; i++ ) { - const int x_ = x + rng( -1, +1 ); - const int y_ = y + rng( -1, +1 ); - if( is_empty( {x_, y_, get_levz()} ) ) { - // invert direction, as game::knockback needs - // the source of the force that knocks back - kbx = -x_ + x + x; - kby = -y_ + y + y; - break; - } + const auto valid_location = [&]( const tripoint & p ) { + return g->is_empty( p ); + }; + if( const cata::optional pos = random_point( m.points_in_radius( p, 2 ), + valid_location ) ) { + kbx = -pos->x + x + x; + kby = -pos->y + y + y; } const tripoint kbp( kbx, kby, p.z ); + if( kbp == p ) { + // cant pushback any creatures anywhere, that means the door cant close. + return false; + } const bool can_see = u.sees( tripoint( x, y, p.z ) ); player *npc_or_player = critter_at( tripoint( x, y, p.z ), false ); if( npc_or_player != nullptr ) {