Skip to content

Commit

Permalink
casting a spell casts the wrong spell (#76137)
Browse files Browse the repository at this point in the history
* Fix an issue where trying to cast a spell causes
the wrong spell to be cast
currently selecting a spell in a long sorted spell
list will cast the wrong spell due to select spell returing a sorted
spell id while handle_action.cpp looks only at the unsorted spells

fixes #76042
Fix an issue where trying to cast a spell causes
the wrong spell to be cast

* Apply suggestions from automated code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* solve issue with closing spell menu with no spell
selected
streamline the feature and fix a bug

* removed an unwanted code comment from
the previous commit

* Apply suggestions from automated code review

local astyle somehow missed those

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
ThePotatomancer and github-actions[bot] authored Sep 5, 2024
1 parent 94b346a commit 5d5ef55
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
8 changes: 3 additions & 5 deletions src/handle_action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1778,13 +1778,11 @@ static void cast_spell()
}
}

const int spell_index = player_character.magic->select_spell( player_character );
if( spell_index < 0 ) {
spell &sp = player_character.magic->select_spell( player_character );
// if no spell was selected
if( sp.id().is_null() ) {
return;
}

spell &sp = *player_character.magic->get_spells()[spell_index];

player_character.cast_spell( sp, false, std::nullopt );
}

Expand Down
11 changes: 7 additions & 4 deletions src/magic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2840,7 +2840,7 @@ int known_magic::get_invlet( const spell_id &sp, std::set<int> &used_invlets )
return 0;
}

int known_magic::select_spell( Character &guy )
spell &known_magic::select_spell( Character &guy )
{
std::vector<spell *> known_spells_sorted = get_spells();

Expand Down Expand Up @@ -2933,8 +2933,12 @@ int known_magic::select_spell( Character &guy )
spell_menu.query( true, 50, true );

casting_ignore = static_cast<spellcasting_callback *>( spell_menu.callback )->casting_ignore;

return spell_menu.ret;
if( spell_menu.ret < 0 ) {
static spell null_spell_reference( spell_id::NULL_ID() );
return null_spell_reference;
}
spell *selected_spell = known_spells_sorted[spell_menu.ret];
return *selected_spell;
}

void known_magic::on_mutation_gain( const trait_id &mid, Character &guy )
Expand Down Expand Up @@ -2991,7 +2995,6 @@ static std::string color_number( const float num )
return colorize( "0", c_white );
}
}

static void draw_spellbook_info( const spell_type &sp )
{
const spell fake_spell( sp.id );
Expand Down
4 changes: 2 additions & 2 deletions src/magic.h
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,8 @@ class known_magic
// gets the spell associated with the spell_id to be edited
spell &get_spell( const spell_id &sp );
// opens up a ui that the Character can choose a spell from
// returns the index of the spell in the vector of spells
int select_spell( Character &guy );
// returns the selected spell
spell &select_spell( Character &guy );
// get all known spells
std::vector<spell *> get_spells();
// directly get the character known spells
Expand Down

0 comments on commit 5d5ef55

Please sign in to comment.