Skip to content

Commit

Permalink
Fix things
Browse files Browse the repository at this point in the history
What's the point of having a definition for infinite charges if it's not used?

Reorganizes ammo_count_for function
  • Loading branch information
KheirFerrum committed Apr 25, 2023
1 parent ee7988b commit 1ac631a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 31 deletions.
51 changes: 23 additions & 28 deletions src/character_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1179,43 +1179,38 @@ std::vector<item_location> find_reloadables( const Character &who )

int ammo_count_for( const Character &who, const item &gun )
{
int ret = item::INFINITE_CHARGES;
if( !gun.is_gun() ) {
return ret;
return item::INFINITE_CHARGES;
}
int ammo_drain = gun.ammo_required();
int energy_drain = gun.get_gun_ups_drain();

int required = gun.ammo_required();
units::energy power = units::from_kilojoule( who.charges_of( itype_UPS ) );
int total_ammo = gun.ammo_remaining();
const std::vector<item_location> inv_ammo = find_ammo_items_or_mags( who, gun, true, -1 );

if( required > 0 ) {
int total_ammo = 0;
total_ammo += gun.ammo_remaining();
bool has_mag = gun.magazine_integral();

bool has_mag = gun.magazine_integral();

const auto found_ammo = find_ammo_items_or_mags( who, gun, true, -1 );
int loose_ammo = 0;
for( const auto &ammo : found_ammo ) {
if( ammo->is_magazine() ) {
has_mag = true;
total_ammo += ammo->ammo_remaining();
} else if( ammo->is_ammo() ) {
loose_ammo += ammo->charges;
}
}

if( has_mag ) {
total_ammo += loose_ammo;
for( const item_location &it : inv_ammo ) {
if( it->is_magazine() ) {
total_ammo += it->ammo_remaining();
} else if( it->is_ammo() && has_mag ) {
total_ammo += it->count();
}

ret = std::min( ret, total_ammo / required );
}

int ups_drain = gun.get_gun_ups_drain();
if( ups_drain > 0 ) {
ret = std::min( ret, who.charges_of( itype_UPS ) / ups_drain );
if( ammo_drain > 0 && energy_drain > 0 ) {
// Both UPS and ammo, lower is limiting.
return std::min( total_ammo, power / units::from_kilojoule( energy_drain ) );
} else if( energy_drain > 0 ) {
//Only one of the two, it is limiting.
return power / units::from_kilojoule( energy_drain );
} else if( ammo_drain > 0 ) {
return total_ammo;
} else {
// Effectively infinite ammo.
return item::INFINITE_CHARGES;
}

return ret;
}

void show_skill_capped_notice( const Character &who, const skill_id &id )
Expand Down
2 changes: 1 addition & 1 deletion src/item_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ int shots_remaining( const Character &who, const item &it )
return it.ammo_remaining();
} else {
// Effectively infinite ammo.
return 10;
return item::INFINITE_CHARGES;
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/npc.h
Original file line number Diff line number Diff line change
Expand Up @@ -1391,9 +1391,9 @@ npc *pick_follower();
namespace npc_ai
{
/** Evaluate item as weapon (melee or gun) */
double weapon_value( const Character &who, const item &weap, int ammo = 10 );
double weapon_value( const Character &who, const item &weap, int ammo = item::INFINITE_CHARGES );
/** Evaluates item as a gun */
double gun_value( const Character &who, const item &weap, int ammo = 10 );
double gun_value( const Character &who, const item &weap, int ammo = item::INFINITE_CHARGES );
/** Evaluate item as a melee weapon */
double melee_value( const Character &who, const item &weap );
/** Evaluate unarmed melee value */
Expand Down

0 comments on commit 1ac631a

Please sign in to comment.