Skip to content

Commit

Permalink
Fixed crash when trying to change targeting mode of vehicle turrets (#…
Browse files Browse the repository at this point in the history
…35622)

* Fixed crash on trying to access out-of-bounds element of the vector

* Removed unneeded include
  • Loading branch information
Night-Pryanik authored and ZhilkinSerg committed Nov 22, 2019
1 parent da9ae7c commit 0f93de3
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/turret.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand Down Expand Up @@ -292,16 +293,12 @@ void vehicle::turrets_set_targeting()
{
std::vector<vehicle_part *> turrets;
std::vector<tripoint> locations;
std::vector<vehicle_part *> 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 );
Expand Down Expand Up @@ -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 );
Expand Down

0 comments on commit 0f93de3

Please sign in to comment.