Skip to content

Commit d9fdf6f

Browse files
committed
...and then move evaluate_weapon() to Character
1 parent 841ac6b commit d9fdf6f

File tree

4 files changed

+43
-37
lines changed

4 files changed

+43
-37
lines changed

src/character.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,6 +1197,7 @@ class Character : public Creature, public visitable
11971197
bool can_attack_high() const override;
11981198

11991199
/** NPC-related item rating functions */
1200+
double evaluate_weapon( const item &maybe_weapon ) const;
12001201
double weapon_value( const item &weap, int ammo = 10 ) const; // Evaluates item as a weapon
12011202
double gun_value( const item &weap, int ammo = 10 ) const; // Evaluates item as a gun
12021203
double melee_value( const item &weap ) const; // As above, but only as melee
@@ -1220,6 +1221,8 @@ class Character : public Creature, public visitable
12201221
bool is_dead_state() const override;
12211222

12221223
private:
1224+
double evaluate_weapon_internal( const item &maybe_weapon, bool can_use_gun,
1225+
bool use_silent ) const;
12231226
mutable std::optional<bool> cached_dead_state;
12241227
public:
12251228
void set_part_hp_cur( const bodypart_id &id, int set ) override;

src/melee.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2723,6 +2723,46 @@ int Character::attack_speed( const item &weap ) const
27232723
return std::round( move_cost );
27242724
}
27252725

2726+
double Character::evaluate_weapon( const item &maybe_weapon ) const
2727+
{
2728+
bool can_use_gun = true;
2729+
bool use_silent = false;
2730+
const npc *me_as_npc = dynamic_cast<const npc *>( this );
2731+
if( me_as_npc ) {
2732+
if( me_as_npc->is_player_ally() && !me_as_npc->rules.has_flag( ally_rule::use_guns ) ) {
2733+
can_use_gun = false;
2734+
}
2735+
if( me_as_npc->is_player_ally() && me_as_npc->rules.has_flag( ally_rule::use_silent ) ) {
2736+
use_silent = true;
2737+
}
2738+
}
2739+
return evaluate_weapon_internal( maybe_weapon, can_use_gun, use_silent );
2740+
}
2741+
2742+
double Character::evaluate_weapon_internal( const item &maybe_weapon, bool can_use_gun,
2743+
bool use_silent ) const
2744+
{
2745+
// Needed because evaluation includes electricity via linked cables.
2746+
const map &here = get_map();
2747+
2748+
bool allowed = can_use_gun && maybe_weapon.is_gun() && ( !use_silent || maybe_weapon.is_silent() );
2749+
// According to unmodified evaluation score, NPCs almost always prioritize wielding guns if they have one.
2750+
// This is relatively reasonable, as players can issue commands to NPCs when we do not want them to use ranged weapons.
2751+
// Conversely, we cannot directly issue commands when we want NPCs to prioritize ranged weapons.
2752+
// Note that the scoring method here is different from the 'weapon_value' used elsewhere.
2753+
double val_gun = allowed ? gun_value( maybe_weapon, maybe_weapon.shots_remaining( here,
2754+
this ) ) : 0;
2755+
add_msg_debug( debugmode::DF_NPC_ITEMAI,
2756+
"%s %s valued at <color_light_cyan>%1.2f as a ranged weapon to wield</color>.",
2757+
disp_name( true ), maybe_weapon.type->get_id().str(), val_gun );
2758+
double val_melee = melee_value( maybe_weapon );
2759+
add_msg_debug( debugmode::DF_NPC_ITEMAI,
2760+
"%s %s valued at <color_light_cyan>%1.2f as a melee weapon to wield</color>.", disp_name( true ),
2761+
maybe_weapon.type->get_id().str(), val_melee );
2762+
double val = std::max( val_gun, val_melee );
2763+
return val;
2764+
}
2765+
27262766
double Character::weapon_value( const item &weap, int ammo ) const
27272767
{
27282768
if( is_wielding( weap ) || ( !get_wielded_item() && weap.is_null() ) ) {

src/npc.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,12 +1131,6 @@ class npc : public Character
11311131
int evaluate_sleep_spot( tripoint_bub_ms p );
11321132
// Returns true if did something and we should end turn
11331133
bool scan_new_items();
1134-
// Returns score for how well this weapon might kill things
1135-
double evaluate_weapon( const item &maybe_weapon ) const;
1136-
private:
1137-
// Returns score for how well this weapon might kill things
1138-
double evaluate_weapon_internal( const item &maybe_weapon, bool can_use_gun,
1139-
bool use_silent ) const;
11401134
public:
11411135
// Returns best weapon. Can return null (fists)
11421136
item *evaluate_best_weapon() const;

src/npcmove.cpp

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4133,37 +4133,6 @@ bool npc::do_player_activity()
41334133
return moves != old_moves;
41344134
}
41354135

4136-
double npc::evaluate_weapon( const item &maybe_weapon ) const
4137-
{
4138-
const bool can_use_gun = !is_player_ally() || rules.has_flag( ally_rule::use_guns );
4139-
const bool use_silent = is_player_ally() && rules.has_flag( ally_rule::use_silent );
4140-
return evaluate_weapon_internal( maybe_weapon, can_use_gun, use_silent );
4141-
}
4142-
4143-
double npc::evaluate_weapon_internal( const item &maybe_weapon, bool can_use_gun,
4144-
bool use_silent ) const
4145-
{
4146-
// Needed because evaluation includes electricity via linked cables.
4147-
const map &here = get_map();
4148-
4149-
bool allowed = can_use_gun && maybe_weapon.is_gun() && ( !use_silent || maybe_weapon.is_silent() );
4150-
// According to unmodified evaluation score, NPCs almost always prioritize wielding guns if they have one.
4151-
// This is relatively reasonable, as players can issue commands to NPCs when we do not want them to use ranged weapons.
4152-
// Conversely, we cannot directly issue commands when we want NPCs to prioritize ranged weapons.
4153-
// Note that the scoring method here is different from the 'weapon_value' used elsewhere.
4154-
double val_gun = allowed ? gun_value( maybe_weapon, maybe_weapon.shots_remaining( here,
4155-
this ) ) : 0;
4156-
add_msg_debug( debugmode::DF_NPC_ITEMAI,
4157-
"%s %s valued at <color_light_cyan>%1.2f as a ranged weapon to wield</color>.",
4158-
disp_name( true ), maybe_weapon.type->get_id().str(), val_gun );
4159-
double val_melee = melee_value( maybe_weapon );
4160-
add_msg_debug( debugmode::DF_NPC_ITEMAI,
4161-
"%s %s valued at <color_light_cyan>%1.2f as a melee weapon to wield</color>.", disp_name( true ),
4162-
maybe_weapon.type->get_id().str(), val_melee );
4163-
double val = std::max( val_gun, val_melee );
4164-
return val;
4165-
}
4166-
41674136
item *npc::evaluate_best_weapon() const
41684137
{
41694138
bool can_use_gun = !is_player_ally() || rules.has_flag( ally_rule::use_guns );

0 commit comments

Comments
 (0)