From 41d97957d583faef729a4c5e16044521e1d4a7c7 Mon Sep 17 00:00:00 2001 From: ThePotatomancer Date: Mon, 2 Sep 2024 08:47:05 +0300 Subject: [PATCH 1/5] 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 --- src/magic.cpp | 18 +++++++++++++++++- src/magic.h | 2 ++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/src/magic.cpp b/src/magic.cpp index bf34c56064ba4..30f91352ba6d0 100644 --- a/src/magic.cpp +++ b/src/magic.cpp @@ -1,4 +1,5 @@ #include "magic.h" +#include "magic.h" #include #include @@ -2238,6 +2239,19 @@ std::vector known_magic::get_spells() return spells; } +int known_magic::get_spell_index( const spell_id& sp ) +{ + int current_index = -1, result_index = -1; + for (auto& spell_pair : spellbook) { + current_index++; + if (spell_pair.first == sp) { + result_index = current_index; + break; + } + } + return result_index; +} + int known_magic::available_mana() const { return mana; @@ -2906,7 +2920,9 @@ int known_magic::select_spell( Character &guy ) casting_ignore = static_cast( spell_menu.callback )->casting_ignore; - return spell_menu.ret; + spell* selected_spell = known_spells_sorted[spell_menu.ret]; + int original_spell_index = get_spell_index(selected_spell->id()); + return original_spell_index; } void known_magic::on_mutation_gain( const trait_id &mid, Character &guy ) diff --git a/src/magic.h b/src/magic.h index 93b94b343f889..b50374428fb16 100644 --- a/src/magic.h +++ b/src/magic.h @@ -688,6 +688,8 @@ class known_magic std::vector spells() const; // gets the spell associated with the spell_id to be edited spell &get_spell( const spell_id &sp ); + // gets the index of a spell in the get_spells vector + int get_spell_index(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 ); From c6aea82970117b4db4de4ed681849e791b5b0b01 Mon Sep 17 00:00:00 2001 From: ThePotatomancer <52547005+ThePotatomancer@users.noreply.github.com> Date: Mon, 2 Sep 2024 09:11:31 +0300 Subject: [PATCH 2/5] Apply suggestions from automated code review Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --- src/magic.cpp | 10 +++++----- src/magic.h | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/magic.cpp b/src/magic.cpp index 30f91352ba6d0..592bc3757cce0 100644 --- a/src/magic.cpp +++ b/src/magic.cpp @@ -2239,12 +2239,12 @@ std::vector known_magic::get_spells() return spells; } -int known_magic::get_spell_index( const spell_id& sp ) +int known_magic::get_spell_index( const spell_id &sp ) { int current_index = -1, result_index = -1; - for (auto& spell_pair : spellbook) { + for( auto &spell_pair : spellbook ) { current_index++; - if (spell_pair.first == sp) { + if( spell_pair.first == sp ) { result_index = current_index; break; } @@ -2920,8 +2920,8 @@ int known_magic::select_spell( Character &guy ) casting_ignore = static_cast( spell_menu.callback )->casting_ignore; - spell* selected_spell = known_spells_sorted[spell_menu.ret]; - int original_spell_index = get_spell_index(selected_spell->id()); + spell *selected_spell = known_spells_sorted[spell_menu.ret]; + int original_spell_index = get_spell_index( selected_spell->id() ); return original_spell_index; } diff --git a/src/magic.h b/src/magic.h index b50374428fb16..23136c89007d1 100644 --- a/src/magic.h +++ b/src/magic.h @@ -689,7 +689,7 @@ class known_magic // gets the spell associated with the spell_id to be edited spell &get_spell( const spell_id &sp ); // gets the index of a spell in the get_spells vector - int get_spell_index(const spell_id& sp); + int get_spell_index( 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 ); From 2b0a3166fcb28cbab83bc2f18a601c35fd082c4e Mon Sep 17 00:00:00 2001 From: ThePotatomancer Date: Mon, 2 Sep 2024 15:09:22 +0300 Subject: [PATCH 3/5] solve issue with closing spell menu with no spell selected streamline the feature and fix a bug --- src/handle_action.cpp | 9 ++++----- src/magic.cpp | 26 +++++++------------------- src/magic.h | 6 ++---- 3 files changed, 13 insertions(+), 28 deletions(-) diff --git a/src/handle_action.cpp b/src/handle_action.cpp index 270fd74b2219b..c9e8b8f401fac 100644 --- a/src/handle_action.cpp +++ b/src/handle_action.cpp @@ -1781,13 +1781,12 @@ 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]; - + // spell &sp = player_character.magic->get_spell( selected_spell_id ); player_character.cast_spell( sp, false, std::nullopt ); } diff --git a/src/magic.cpp b/src/magic.cpp index 30f91352ba6d0..bc375420ccf18 100644 --- a/src/magic.cpp +++ b/src/magic.cpp @@ -1,5 +1,4 @@ #include "magic.h" -#include "magic.h" #include #include @@ -2239,19 +2238,6 @@ std::vector known_magic::get_spells() return spells; } -int known_magic::get_spell_index( const spell_id& sp ) -{ - int current_index = -1, result_index = -1; - for (auto& spell_pair : spellbook) { - current_index++; - if (spell_pair.first == sp) { - result_index = current_index; - break; - } - } - return result_index; -} - int known_magic::available_mana() const { return mana; @@ -2826,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(); @@ -2919,10 +2905,12 @@ int known_magic::select_spell( Character &guy ) spell_menu.query( true, 50, true ); casting_ignore = static_cast( spell_menu.callback )->casting_ignore; - + 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]; - int original_spell_index = get_spell_index(selected_spell->id()); - return original_spell_index; + return *selected_spell; } void known_magic::on_mutation_gain( const trait_id &mid, Character &guy ) @@ -2979,7 +2967,7 @@ 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 b50374428fb16..61057e5b20f63 100644 --- a/src/magic.h +++ b/src/magic.h @@ -688,11 +688,9 @@ class known_magic std::vector spells() const; // gets the spell associated with the spell_id to be edited spell &get_spell( const spell_id &sp ); - // gets the index of a spell in the get_spells vector - int get_spell_index(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 From f4375988c9b1b3fd85ac618096b20533e0fe999e Mon Sep 17 00:00:00 2001 From: ThePotatomancer Date: Mon, 2 Sep 2024 15:18:59 +0300 Subject: [PATCH 4/5] removed an unwanted code comment from the previous commit --- src/handle_action.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/handle_action.cpp b/src/handle_action.cpp index c9e8b8f401fac..a2feae6fdd25d 100644 --- a/src/handle_action.cpp +++ b/src/handle_action.cpp @@ -1786,7 +1786,6 @@ static void cast_spell() if( sp.id().is_null() ) { return; } - // spell &sp = player_character.magic->get_spell( selected_spell_id ); player_character.cast_spell( sp, false, std::nullopt ); } From 2cf44eddcb08e973c8a15b5cf8e61eea6f326ee4 Mon Sep 17 00:00:00 2001 From: ThePotatomancer <52547005+ThePotatomancer@users.noreply.github.com> Date: Mon, 2 Sep 2024 15:50:25 +0300 Subject: [PATCH 5/5] 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> --- src/magic.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/magic.cpp b/src/magic.cpp index bc375420ccf18..0700660486ece 100644 --- a/src/magic.cpp +++ b/src/magic.cpp @@ -2906,10 +2906,10 @@ spell &known_magic::select_spell( Character &guy ) casting_ignore = static_cast( spell_menu.callback )->casting_ignore; if( spell_menu.ret < 0 ) { - static spell null_spell_reference(spell_id::NULL_ID()); + static spell null_spell_reference( spell_id::NULL_ID() ); return null_spell_reference; } - spell* selected_spell = known_spells_sorted[spell_menu.ret]; + spell *selected_spell = known_spells_sorted[spell_menu.ret]; return *selected_spell; } @@ -2967,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 );