diff --git a/src/magic.cpp b/src/magic.cpp index e64921c4aadc4..73f4611e11b24 100644 --- a/src/magic.cpp +++ b/src/magic.cpp @@ -1343,11 +1343,6 @@ float spell::spell_fail( const Character &guy ) const if( has_flag( spell_flag::NO_FAIL ) ) { return 0.0f; } - if( type->magic_type.has_value() && - type->magic_type.value()->failure_chance_formula_id.has_value() ) { - const_dialogue d( get_const_talker_for( guy ), nullptr ); - return type->magic_type.value()->failure_chance_formula_id.value()->eval( d ); - } const bool is_psi = has_flag( spell_flag::PSIONIC ); @@ -1390,15 +1385,26 @@ float spell::spell_fail( const Character &guy ) const static_cast( 45 ) ); } } + bool has_type_fail_chance = type->magic_type.has_value() && + type->magic_type.value()->failure_chance_formula_id.has_value(); // add an if statement in here because sufficiently large numbers will definitely overflow because of exponents - if( ( effective_skill > 30.0f && !is_psi ) || ( psi_effective_skill > 40.0f && is_psi ) ) { - return 0.0f; - } else if( ( effective_skill < 0.0f && !is_psi ) || ( psi_effective_skill < 0.0f && is_psi ) ) { - return 1.0f; + if( !has_type_fail_chance ) { + if( ( effective_skill > 30.0f && !is_psi ) || ( psi_effective_skill > 40.0f && is_psi ) ) { + return 0.0f; + } else if( ( effective_skill < 0.0f && !is_psi ) || ( psi_effective_skill < 0.0f && is_psi ) ) { + return 1.0f; + } } - float fail_chance = std::pow( ( effective_skill - 30.0f ) / 30.0f, 2 ); - float psi_fail_chance = std::pow( ( psi_effective_skill - 40.0f ) / 40.0f, 2 ); + float fail_chance = 0; + if( has_type_fail_chance ) { + const_dialogue d( get_const_talker_for( guy ), nullptr ); + fail_chance = type->magic_type.value()->failure_chance_formula_id.value()->eval( d ); + } else if( is_psi ) { + fail_chance = std::pow( ( psi_effective_skill - 40.0f ) / 40.0f, 2 ); + } else { + fail_chance = std::pow( ( effective_skill - 30.0f ) / 30.0f, 2 ); + } if( !is_psi ) { if( has_flag( spell_flag::SOMATIC ) && @@ -1429,10 +1435,9 @@ float spell::spell_fail( const Character &guy ) const return 1.0f; } fail_chance /= 1.0f - concentration_loss; - psi_fail_chance /= 1.0f - concentration_loss; } - return clamp( is_psi ? psi_fail_chance : fail_chance, 0.0f, 1.0f ); + return clamp( fail_chance, 0.0f, 1.0f ); } std::string spell::colorized_fail_percent( const Character &guy ) const