Skip to content

Commit

Permalink
Don't need to nest it so deep.
Browse files Browse the repository at this point in the history
  • Loading branch information
KheirFerrum committed Mar 30, 2023
1 parent d80815d commit 33edcc9
Showing 1 changed file with 79 additions and 81 deletions.
160 changes: 79 additions & 81 deletions src/bionics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,111 +520,109 @@ void npc::check_or_use_weapon_cbm()
}

// There's no point in checking the bionics if they can't unwield what they have.
if( !weapon.has_flag( flag_NO_UNWIELD ) && cbm_weapon_index < 0 ) {
if( !avail_toggle_cbms.empty() ) {
int melee_index = -1;
for( int i : avail_toggle_cbms ) {
bionic &bio = ( *my_bionics )[ i ];
if( free_power > bio.info().power_activate ) {
item cbm_fake = item( bio.info().fake_item );
if( bio.ammo_count > 0 ) {
cbm_fake.ammo_set( bio.ammo_loaded, bio.ammo_count );
}
if( !weapon.has_flag( flag_NO_UNWIELD ) && cbm_weapon_index < 0 && !avail_toggle_cbms.empty() ) {
int melee_index = -1;
for( int i : avail_toggle_cbms ) {
bionic &bio = ( *my_bionics )[ i ];
if( free_power > bio.info().power_activate ) {
item cbm_fake = item( bio.info().fake_item );
if( bio.ammo_count > 0 ) {
cbm_fake.ammo_set( bio.ammo_loaded, bio.ammo_count );
}

bool not_allowed = ( !rules.has_flag( ally_rule::use_guns ) && cbm_fake.is_gun() ) ||
( rules.has_flag( ally_rule::use_silent ) && !cbm_fake.is_silent() );
if( not_allowed ) {
continue;
}
bool not_allowed = ( !rules.has_flag( ally_rule::use_guns ) && cbm_fake.is_gun() ) ||
( rules.has_flag( ally_rule::use_silent ) && !cbm_fake.is_silent() );
if( not_allowed ) {
continue;
}

if( melee_index >= 0 ) {
// Previous iteration chose a CBM. Compare previous and current.
const item cbm_best_fake = item( ( *my_bionics )[ melee_index ].info().fake_item );
if( melee_index >= 0 ) {
// Previous iteration chose a CBM. Compare previous and current.
const item cbm_best_fake = item( ( *my_bionics )[ melee_index ].info().fake_item );

if( npc_ai::weapon_value( *this, cbm_best_fake, cbm_best_fake.shots_remaining( free_power ) )
< npc_ai::weapon_value( *this, cbm_fake, cbm_fake.shots_remaining( free_power ) ) ) {
// Current is better, update index.
melee_index = i;
}
} else {
const units::energy ups_charges = units::from_kilojoule( charges_of( itype_UPS ) );
if( npc_ai::weapon_value( *this, cbm_best_fake, cbm_best_fake.shots_remaining( free_power ) )
< npc_ai::weapon_value( *this, cbm_fake, cbm_fake.shots_remaining( free_power ) ) ) {
// Current is better, update index.
melee_index = i;
}
} else {
const units::energy ups_charges = units::from_kilojoule( charges_of( itype_UPS ) );

if( npc_ai::weapon_value( *this, weapon, weapon.shots_remaining( ups_charges ) ) <
npc_ai::weapon_value( *this, cbm_fake, cbm_fake.shots_remaining( free_power ) ) ) {
melee_index = i;
}
if( npc_ai::weapon_value( *this, weapon, weapon.shots_remaining( ups_charges ) ) <
npc_ai::weapon_value( *this, cbm_fake, cbm_fake.shots_remaining( free_power ) ) ) {
melee_index = i;
}
}
}
}

if( melee_index > 0 ) {
// Decided on a bionic, swap to it.
if( is_armed() ) {
stow_item( weapon );
}
activate_bionic_by_id( ( *my_bionics )[ melee_index ].id );
if( get_player_character().sees( pos() ) ) {
add_msg( m_info, _( "%s activates their %s." ), disp_name(),
( *my_bionics )[ melee_index ].info().name );
}
clear_npc_ai_info_cache( "weapon_value" );
cbm_weapon_index = melee_index;
if( melee_index > 0 ) {
// Decided on a bionic, swap to it.
if( is_armed() ) {
stow_item( weapon );
}
activate_bionic_by_id( ( *my_bionics )[ melee_index ].id );
if( get_player_character().sees( pos() ) ) {
add_msg( m_info, _( "%s activates their %s." ), disp_name(),
( *my_bionics )[ melee_index ].info().name );
}
clear_npc_ai_info_cache( "weapon_value" );
cbm_weapon_index = melee_index;
}

}

if( cbm_active_index < 0 ) {
if( !avail_active_cbms.empty() ) {
int ranged_index = -1;
bool wield_gun = weapon.is_gun();
for( int i : avail_active_cbms ) {
bionic &bio = ( *my_bionics )[ i ];
const item cbm_weapon = item( bio.info().fake_item );

bool not_allowed = !rules.has_flag( ally_rule::use_guns ) ||
( rules.has_flag( ally_rule::use_silent ) && !cbm_weapon.is_silent() );
if( is_player_ally() && not_allowed ) {
continue;
}
if( cbm_active_index < 0 && !avail_active_cbms.empty() ) {
int ranged_index = -1;
bool wield_gun = weapon.is_gun();
for( int i : avail_active_cbms ) {
bionic &bio = ( *my_bionics )[ i ];
const item cbm_weapon = item( bio.info().fake_item );

// Simpler than weapons because they're not real items and cannot be reloaded.
// Have fun changing this in the future.
int cbm_ammo = free_power / bio.info().power_activate;
bool not_allowed = !rules.has_flag( ally_rule::use_guns ) ||
( rules.has_flag( ally_rule::use_silent ) && !cbm_weapon.is_silent() );
if( is_player_ally() && not_allowed ) {
continue;
}

if( ranged_index > 0 ) {
// Previous iteration chose a CBM, compare them.
const item b_cbm_weapon = item( ( *my_bionics )[ ranged_index ].info().fake_item );
// Simpler than weapons because they're not real items and cannot be reloaded.
// Have fun changing this in the future.
int cbm_ammo = free_power / bio.info().power_activate;

int b_cbm_ammo = free_power / ( *my_bionics )[ranged_index].info().power_activate;
if( ranged_index > 0 ) {
// Previous iteration chose a CBM, compare them.
const item b_cbm_weapon = item( ( *my_bionics )[ ranged_index ].info().fake_item );

if( npc_ai::weapon_value( *this, b_cbm_weapon, b_cbm_ammo ) < npc_ai::weapon_value( *this,
cbm_weapon, cbm_ammo ) ) {
// New one is better, update.
ranged_index = i;
}
int b_cbm_ammo = free_power / ( *my_bionics )[ranged_index].info().power_activate;

} else if( wield_gun ) {
// For melee we keep it in reserve anyway, but ranged we're using either it or this one.
// Right now no BIONIC_GUNS are melee weapons, unlike BIONIC_WEAPONS and the bionic shotgun.
const units::energy ups_charges = units::from_kilojoule( charges_of( itype_UPS ) );
if( npc_ai::weapon_value( *this, b_cbm_weapon, b_cbm_ammo ) < npc_ai::weapon_value( *this,
cbm_weapon, cbm_ammo ) ) {
// New one is better, update.
ranged_index = i;
}

if( npc_ai::weapon_value( *this, weapon, weapon.shots_remaining( ups_charges ) ) <
npc_ai::weapon_value( *this, cbm_weapon, cbm_ammo ) ) {
ranged_index = i;
}
} else if( wield_gun ) {
// For melee we keep it in reserve anyway, but ranged we're using either it or this one.
// Right now no BIONIC_GUNS are melee weapons, unlike BIONIC_WEAPONS and the bionic shotgun.
const units::energy ups_charges = units::from_kilojoule( charges_of( itype_UPS ) );

} else {
// If it's not a gun, then we only need to compare the CBMs as
// You can fire a activated CBM without the need to equip/unequip anything.
if( npc_ai::weapon_value( *this, weapon, weapon.shots_remaining( ups_charges ) ) <
npc_ai::weapon_value( *this, cbm_weapon, cbm_ammo ) ) {
ranged_index = i;
}
}
cbm_active_index = ranged_index;
if( cbm_active_index >= 0 ) {
cbm_fake_active = item( ( *my_bionics )[cbm_active_index].info().fake_item );

} else {
// If it's not a gun, then we only need to compare the CBMs as
// You can fire a activated CBM without the need to equip/unequip anything.
ranged_index = i;
}
}
cbm_active_index = ranged_index;
if( cbm_active_index >= 0 ) {
cbm_fake_active = item( ( *my_bionics )[cbm_active_index].info().fake_item );
}
}

}

// Why put this in a Big Switch? Why not let bionics have pointers to
Expand Down

0 comments on commit 33edcc9

Please sign in to comment.