diff --git a/src/handle_action.cpp b/src/handle_action.cpp index 270fd74b2219b..a2feae6fdd25d 100644 --- a/src/handle_action.cpp +++ b/src/handle_action.cpp @@ -1781,13 +1781,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 ); } diff --git a/src/magic.cpp b/src/magic.cpp index bf34c56064ba4..0700660486ece 100644 --- a/src/magic.cpp +++ b/src/magic.cpp @@ -2812,7 +2812,7 @@ int known_magic::get_invlet( const spell_id &sp, std::set &used_invlets ) return 0; } -int known_magic::select_spell( Character &guy ) +spell &known_magic::select_spell( Character &guy ) { std::vector known_spells_sorted = get_spells(); @@ -2905,8 +2905,12 @@ int known_magic::select_spell( Character &guy ) spell_menu.query( true, 50, true ); casting_ignore = static_cast( 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 ) @@ -2963,7 +2967,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 ); diff --git a/src/magic.h b/src/magic.h index 93b94b343f889..61057e5b20f63 100644 --- a/src/magic.h +++ b/src/magic.h @@ -689,8 +689,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 get_spells(); // directly get the character known spells