From f9882e6b08c499c5a03b0687c0276978318414de Mon Sep 17 00:00:00 2001 From: Brambor Date: Thu, 2 Jan 2025 19:10:21 +0100 Subject: [PATCH] Recast spell --- data/raw/keybindings.json | 6 ++++++ src/action.cpp | 3 +++ src/action.h | 2 ++ src/game.cpp | 1 + src/handle_action.cpp | 16 ++++++++++++++-- src/magic.h | 2 ++ 6 files changed, 28 insertions(+), 2 deletions(-) diff --git a/data/raw/keybindings.json b/data/raw/keybindings.json index 3c52c01ce3568..16bb55d143885 100644 --- a/data/raw/keybindings.json +++ b/data/raw/keybindings.json @@ -3281,6 +3281,12 @@ "category": "DEFAULTMODE", "bindings": [ { "input_method": "keyboard_any", "key": "]" } ] }, + { + "type": "keybinding", + "id": "recast_spell", + "name": "Repeat last supernatural ability", + "category": "DEFAULTMODE" + }, { "type": "keybinding", "id": "diary", diff --git a/src/action.cpp b/src/action.cpp index d974b90c7c6e1..e80207e46cb25 100644 --- a/src/action.cpp +++ b/src/action.cpp @@ -256,6 +256,8 @@ std::string action_ident( action_id act ) return "fire_burst"; case ACTION_CAST_SPELL: return "cast_spell"; + case ACTION_RECAST_SPELL: + return "recast_spell"; case ACTION_SELECT_FIRE_MODE: return "select_fire_mode"; case ACTION_SELECT_DEFAULT_AMMO: @@ -979,6 +981,7 @@ action_id handle_action_menu() REGISTER_ACTION( ACTION_RELOAD_WEAPON ); REGISTER_ACTION( ACTION_RELOAD_WIELDED ); REGISTER_ACTION( ACTION_CAST_SPELL ); + REGISTER_ACTION( ACTION_RECAST_SPELL ); REGISTER_ACTION( ACTION_SELECT_FIRE_MODE ); REGISTER_ACTION( ACTION_SELECT_DEFAULT_AMMO ); REGISTER_ACTION( ACTION_THROW ); diff --git a/src/action.h b/src/action.h index c7c0afe125e91..daf6a9dd0ebed 100644 --- a/src/action.h +++ b/src/action.h @@ -188,6 +188,8 @@ enum action_id : int { ACTION_SELECT_DEFAULT_AMMO, /** Cast a spell (only if any spells are known) */ ACTION_CAST_SPELL, + /** Recast last spell */ + ACTION_RECAST_SPELL, /** Open the insert-item menu */ ACTION_INSERT_ITEM, /** Unload container in a given direction */ diff --git a/src/game.cpp b/src/game.cpp index 17c019c62aef6..4e52c6e391deb 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -2658,6 +2658,7 @@ input_context get_default_mode_input_context() ctxt.register_action( "throw_wielded" ); ctxt.register_action( "fire" ); ctxt.register_action( "cast_spell" ); + ctxt.register_action( "recast_spell" ); ctxt.register_action( "fire_burst" ); ctxt.register_action( "select_fire_mode" ); ctxt.register_action( "select_default_ammo" ); diff --git a/src/handle_action.cpp b/src/handle_action.cpp index 34e395225f3fa..2f2d398c1bbdc 100644 --- a/src/handle_action.cpp +++ b/src/handle_action.cpp @@ -1831,7 +1831,7 @@ static void open_movement_mode_menu() } } -static void cast_spell() +static void cast_spell( bool recast_spell = false ) { Character &player_character = get_player_character(); player_character.magic->clear_opens_spellbook_data(); @@ -1873,11 +1873,19 @@ static void cast_spell() } } - spell &sp = player_character.magic->select_spell( player_character ); + if( recast_spell && player_character.magic->last_spell.is_null() ) { + popup( _( "Cast a spell first" ) ); + return; + } + + spell &sp = recast_spell + ? player_character.magic->get_spell( player_character.magic->last_spell ) + : player_character.magic->select_spell( player_character ); // if no spell was selected if( sp.id().is_null() ) { return; } + player_character.magic->last_spell = sp.id(); player_character.cast_spell( sp, false, std::nullopt ); } @@ -2638,6 +2646,10 @@ bool game::do_regular_action( action_id &act, avatar &player_character, cast_spell(); break; + case ACTION_RECAST_SPELL: + cast_spell( true ); + break; + case ACTION_FIRE_BURST: { if( weapon ) { if( weapon->gun_set_mode( gun_mode_BURST ) || weapon->gun_set_mode( gun_mode_AUTO ) ) { diff --git a/src/magic.h b/src/magic.h index 406f1df015aaa..1d75ff118f51e 100644 --- a/src/magic.h +++ b/src/magic.h @@ -729,6 +729,8 @@ class known_magic spell &select_spell( Character &guy ); // get all known spells std::vector get_spells(); + // Last spell casted + spell_id last_spell = spell_id::NULL_ID(); // NOLINT(cata-serialize) // directly get the character known spells std::map &get_spellbook() { return spellbook;