From 0f93de36efd410c507e4ec2b2bbecc557e6949cd Mon Sep 17 00:00:00 2001 From: Anton Burmistrov Date: Fri, 22 Nov 2019 13:48:29 +0400 Subject: [PATCH] Fixed crash when trying to change targeting mode of vehicle turrets (#35622) * Fixed crash on trying to access out-of-bounds element of the vector * Removed unneeded include --- src/turret.cpp | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/turret.cpp b/src/turret.cpp index c25574c272647..d6860a2fc3932 100644 --- a/src/turret.cpp +++ b/src/turret.cpp @@ -23,6 +23,7 @@ #include "debug.h" #include "optional.h" #include "enums.h" +#include "vpart_range.h" static const itype_id fuel_type_battery( "battery" ); const efftype_id effect_on_roof( "on_roof" ); @@ -292,16 +293,12 @@ void vehicle::turrets_set_targeting() { std::vector turrets; std::vector locations; - std::vector turret_controls; for( auto &p : parts ) { - if( p.base.is_gun() ) { + if( p.is_turret() ) { turrets.push_back( &p ); locations.push_back( global_part_pos3( p ) ); } - if( part_flag( index_of_part( &p ), "TURRET_CONTROLS" ) ) { - turret_controls.push_back( &p ); - } } pointmenu_cb callback( locations ); @@ -331,11 +328,17 @@ void vehicle::turrets_set_targeting() sel = menu.ret; if( has_part( locations[ sel ], "TURRET_CONTROLS" ) ) { turrets[sel]->enabled = !turrets[sel]->enabled; - turret_controls[sel]->enabled = turrets[sel]->enabled; } else { turrets[sel]->enabled = false; } + for( const vpart_reference &vp : get_avail_parts( "TURRET_CONTROLS" ) ) { + vehicle_part &e = vp.part(); + if( e.mount == turrets[sel]->mount ) { + e.enabled = turrets[sel]->enabled; + } + } + // clear the turret's current targets to prevent unwanted auto-firing tripoint pos = locations[ sel ]; turrets[ sel ]->reset_target( pos );