Skip to content

Commit

Permalink
Magic Infrastructure: Make magic_type spell fail chance still affecte…
Browse files Browse the repository at this point in the history
…d by spell specific flags (#79506)

* Make spell fail chance still affected by spell specific flags

* astyle

* skip auto fail / suceed if magic type defined
  • Loading branch information
b3brodie authored Feb 4, 2025
1 parent 99ffd7c commit 1f38c9b
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/magic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -1390,15 +1385,26 @@ float spell::spell_fail( const Character &guy ) const
static_cast<float>( 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 ) &&
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1f38c9b

Please sign in to comment.