From 41d97957d583faef729a4c5e16044521e1d4a7c7 Mon Sep 17 00:00:00 2001 From: ThePotatomancer Date: Mon, 2 Sep 2024 08:47:05 +0300 Subject: [PATCH] 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 );