Skip to content

Commit

Permalink
Magic Infrastructure: Add failure_chance field (#79470)
Browse files Browse the repository at this point in the history
* Basic implementation

* fix? talker

* astyle

* Add consistency check

* double astyle
  • Loading branch information
b3brodie authored Feb 1, 2025
1 parent 5a3d433 commit acd9d2f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/magic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,12 @@ 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 );

// formula is based on the following:
Expand Down
6 changes: 6 additions & 0 deletions src/magic_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ void magic_type::load( const JsonObject &jo, const std::string_view src )
id.c_str() );
}
optional( jo, was_loaded, "casting_xp_formula_id", casting_xp_formula_id );
optional( jo, was_loaded, "failure_chance_formula_id", failure_chance_formula_id );
optional( jo, was_loaded, "energy_source", energy_source );
if( jo.has_array( "cannot_cast_flags" ) ) {
for( auto &cannot_cast_flag : jo.get_string_array( "cannot_cast_flags" ) ) {
Expand Down Expand Up @@ -80,6 +81,7 @@ void magic_type::serialize( JsonOut &json ) const
json.member( "get_level_formula_id", get_level_formula_id );
json.member( "exp_for_level_formula_id", exp_for_level_formula_id );
json.member( "casting_xp_formula_id", casting_xp_formula_id );
json.member( "failure_chance_formula_id", failure_chance_formula_id );
json.member( "energy_source", energy_source );
json.member( "cannot_cast_flags", cannot_cast_flags, std::set<std::string> {} );
json.member( "cannot_cast_message", cannot_cast_message );
Expand All @@ -106,6 +108,10 @@ void magic_type::check_consistency()
if( m_t.casting_xp_formula_id.has_value() && m_t.casting_xp_formula_id.value()->num_params != 0 ) {
debugmsg( "ERROR: %s casting_xp_formula_id has params that != 0!", m_t.id.c_str() );
}
if( m_t.failure_chance_formula_id.has_value() &&
m_t.failure_chance_formula_id.value()->num_params != 0 ) {
debugmsg( "ERROR: %s failure_chance_formula_id has params that != 0!", m_t.id.c_str() );
}
}
}

Expand Down
1 change: 1 addition & 0 deletions src/magic_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class magic_type
std::optional<jmath_func_id> exp_for_level_formula_id;

std::optional<jmath_func_id> casting_xp_formula_id;
std::optional<jmath_func_id> failure_chance_formula_id;
std::optional<magic_energy_type> energy_source;
std::set<std::string> cannot_cast_flags; // string flags
std::optional<std::string> cannot_cast_message;
Expand Down
2 changes: 2 additions & 0 deletions src/npc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3976,3 +3976,5 @@ std::unique_ptr<talker> get_talker_for( npc *guy )
{
return std::make_unique<talker_npc>( guy );
}


0 comments on commit acd9d2f

Please sign in to comment.