Skip to content

Commit

Permalink
melee: clean up the code slightly
Browse files Browse the repository at this point in the history
Fix some long lines and use std:: functions in place of math.h functions
in a few places.
  • Loading branch information
mlangsdorf committed Apr 18, 2020
1 parent d24b8d3 commit fef593b
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/melee.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ void Character::melee_attack( Creature &t, bool allow_special )
void Character::melee_attack( Creature &t, bool allow_special, const matec_id &force_technique,
bool allow_unarmed )
{
melee::melee_stats.attack_count += 1;
int hit_spread = t.deal_melee_attack( this, hit_roll() );
if( !t.is_player() ) {
// TODO: Per-NPC tracking? Right now monster hit by either npc or player will draw aggro...
Expand All @@ -397,11 +398,13 @@ void Character::melee_attack( Creature &t, bool allow_special, const matec_id &f
auto mons = mounted_creature.get();
if( mons->has_flag( MF_RIDEABLE_MECH ) ) {
if( !mons->check_mech_powered() ) {
add_msg( m_bad, _( "The %s has dead batteries and will not move its arms." ), mons->get_name() );
add_msg( m_bad, _( "The %s has dead batteries and will not move its arms." ),
mons->get_name() );
return;
}
if( mons->type->has_special_attack( "SMASH" ) && one_in( 3 ) ) {
add_msg( m_info, _( "The %s hisses as its hydraulic arm pumps forward!" ), mons->get_name() );
add_msg( m_info, _( "The %s hisses as its hydraulic arm pumps forward!" ),
mons->get_name() );
mattack::smash_specific( mons, &t );
} else {
mons->use_mech_power( -2 );
Expand All @@ -418,7 +421,6 @@ void Character::melee_attack( Creature &t, bool allow_special, const matec_id &f
return;
}

const bool critical_hit = scored_crit( t.dodge_roll(), cur_weapon );
int move_cost = attack_speed( cur_weapon );

if( hit_spread < 0 ) {
Expand Down Expand Up @@ -467,9 +469,11 @@ void Character::melee_attack( Creature &t, bool allow_special, const matec_id &f
// trigger martial arts on-miss effects
martial_arts_data.ma_onmiss_effects( *this );
} else {
melee::melee_stats.hit_count += 1;
// Remember if we see the monster at start - it may change
const bool seen = g->u.sees( t );
// Start of attacks.
const bool critical_hit = scored_crit( t.dodge_roll(), cur_weapon );
damage_instance d;
roll_all_damage( critical_hit, d, false, cur_weapon );

Expand Down Expand Up @@ -511,21 +515,24 @@ void Character::melee_attack( Creature &t, bool allow_special, const matec_id &f
perform_special_attacks( t, dealt_special_dam );
}
t.deal_melee_hit( this, hit_spread, critical_hit, d, dealt_dam );
if( ( cur_weapon.is_null() && ( dealt_dam.type_damage( DT_CUT ) > 0 ||
dealt_dam.type_damage( DT_STAB ) > 0 ) ) || ( dealt_special_dam.type_damage( DT_CUT ) > 0 ||
dealt_special_dam.type_damage( DT_STAB ) > 0 ) ) {
if( dealt_special_dam.type_damage( DT_CUT ) > 0 ||
dealt_special_dam.type_damage( DT_STAB ) > 0 ||
( cur_weapon.is_null() && ( dealt_dam.type_damage( DT_CUT ) > 0 ||
dealt_dam.type_damage( DT_STAB ) > 0 ) ) ) {
if( has_trait( trait_POISONOUS ) ) {
add_msg_if_player( m_good, _( "You poison %s!" ), t.disp_name() );
t.add_effect( effect_poison, 6_turns );
} else if( has_trait( trait_POISONOUS2 ) ) {
add_msg_if_player( m_good, _( "You inject your venom into %s!" ), t.disp_name() );
add_msg_if_player( m_good, _( "You inject your venom into %s!" ),
t.disp_name() );
t.add_effect( effect_badpoison, 6_turns );
}
}

// Make a rather quiet sound, to alert any nearby monsters
if( !is_quiet() ) { // check martial arts silence
sounds::sound( pos(), 8, sounds::sound_t::combat, "whack!" ); //sound generated later
//sound generated later
sounds::sound( pos(), 8, sounds::sound_t::combat, "whack!" );
}
std::string material = "flesh";
if( t.is_monster() ) {
Expand All @@ -551,7 +558,8 @@ void Character::melee_attack( Creature &t, bool allow_special, const matec_id &f
std::string message = melee_message( technique, *this, dealt_dam );
player_hit_message( this, message, t, dam, critical_hit );
} else {
add_msg_player_or_npc( m_good, _( "You hit something." ), _( "<npcname> hits something." ) );
add_msg_player_or_npc( m_good, _( "You hit something." ),
_( "<npcname> hits something." ) );
}

if( !specialmsg.empty() ) {
Expand All @@ -576,7 +584,8 @@ void Character::melee_attack( Creature &t, bool allow_special, const matec_id &f
const int melee = get_skill_level( skill_melee );
/** @EFFECT_STR reduces stamina cost for melee attack with heavier weapons */
const int weight_cost = cur_weapon.weight() / ( 2_gram * std::max( 1, str_cur ) );
const int encumbrance_cost = roll_remainder( ( encumb( bp_arm_l ) + encumb( bp_arm_r ) ) * 2.0f );
const int encumbrance_cost = roll_remainder( ( encumb( bp_arm_l ) + encumb( bp_arm_r ) ) *
2.0f );
const int deft_bonus = hit_spread < 0 && has_trait( trait_DEFT ) ? 50 : 0;
/** @EFFECT_MELEE reduces stamina cost of melee attacks */
const int mod_sta = ( weight_cost + encumbrance_cost - melee - deft_bonus + 50 ) * -1;
Expand Down Expand Up @@ -732,15 +741,17 @@ double Character::crit_chance( float roll_hit, float target_dodge, const item &w

// Chance to get all 3 criticals (a guaranteed critical regardless of hit/dodge)
const double chance_triple = weapon_crit_chance * stat_crit_chance * skill_crit_chance;
// Only check double critical (one that requires hit/dodge comparison) if we have good hit vs dodge
// Only check double critical (one that requires hit/dodge comparison) if we have good
// hit vs dodge
if( roll_hit > target_dodge * 3 / 2 ) {
const double chance_double = 0.5 * (
weapon_crit_chance * stat_crit_chance +
stat_crit_chance * skill_crit_chance +
weapon_crit_chance * skill_crit_chance -
( 3 * chance_triple ) );
// Because chance_double already removed the triples with -( 3 * chance_triple ), chance_triple
// and chance_double are mutually exclusive probabilities and can just be added together.
// Because chance_double already removed the triples with -( 3 * chance_triple ),
// chance_triple and chance_double are mutually exclusive probabilities and can just
// be added together.
return chance_triple + chance_double;
}

Expand Down Expand Up @@ -2352,16 +2363,15 @@ void avatar::steal( npc &target )
mod_moves( -200 );
}

namespace melee
{

/**
* Once the accuracy (sum of modifiers) of an attack has been determined,
* this is used to actually roll the "hit value" of the attack to be compared to dodge.
*/
float melee_hit_range( float accuracy )
float melee::melee_hit_range( float accuracy )
{
return normal_roll( accuracy * 5, 25.0f );
}

namespace melee
{
} // namespace melee

0 comments on commit fef593b

Please sign in to comment.