From: Pasi Kallinen Date: Wed, 23 Feb 2022 08:45:14 +0000 (+0200) Subject: Some spell code reorg X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=e0a83630e14f2f5d3973df4f06602e65fff4352d;p=nethack Some spell code reorg Keep the internal spell array index inside spell.c, and refer to spells outside of it with the otyp id. --- diff --git a/include/spell.h b/include/spell.h index 479283458..4090850b9 100644 --- a/include/spell.h +++ b/include/spell.h @@ -6,6 +6,7 @@ #define SPELL_H #define NO_SPELL 0 +#define UNKNOWN_SPELL (-1) /* spellbook re-use control; used when reading and when polymorphing */ #define MAX_SPELL_STUDY 3 diff --git a/src/apply.c b/src/apply.c index ef73be941..9d16053a7 100644 --- a/src/apply.c +++ b/src/apply.c @@ -1762,7 +1762,7 @@ jump(int magic) /* 0=Physical, otherwise skill level */ /* attempt "jumping" spell if hero has no innate jumping ability */ if (!magic && !Jumping && known_spell(SPE_JUMPING)) - return spelleffects(spell_idx(SPE_JUMPING), FALSE); + return spelleffects(SPE_JUMPING, FALSE); if (!magic && (nolimbs(g.youmonst.data) || slithy(g.youmonst.data))) { /* normally (nolimbs || slithy) implies !Jumping, diff --git a/src/pray.c b/src/pray.c index 11557abb6..d12b848cb 100644 --- a/src/pray.c +++ b/src/pray.c @@ -2008,7 +2008,7 @@ doturn(void) if (!Role_if(PM_CLERIC) && !Role_if(PM_KNIGHT)) { /* Try to use the "turn undead" spell. */ if (known_spell(SPE_TURN_UNDEAD)) - return spelleffects(spell_idx(SPE_TURN_UNDEAD), FALSE); + return spelleffects(SPE_TURN_UNDEAD, FALSE); You("don't know how to turn undead!"); return ECMD_OK; } diff --git a/src/spell.c b/src/spell.c index 2b2a0292c..76de4c57d 100644 --- a/src/spell.c +++ b/src/spell.c @@ -699,9 +699,7 @@ getspell(int* spell_no) if (flags.menu_style == MENU_TRADITIONAL) { /* we know there is at least 1 known spell */ - for (nspells = 1; nspells < MAXSPELL && spellid(nspells) != NO_SPELL; - nspells++) - continue; + nspells = num_spells(); if (nspells == 1) Strcpy(lets, "a"); @@ -742,7 +740,7 @@ docast(void) int spell_no; if (getspell(&spell_no)) - return spelleffects(spell_no, FALSE); + return spelleffects(g.spl_book[spell_no].sp_id, FALSE); return ECMD_OK; } @@ -896,9 +894,12 @@ spell_backfire(int spell) return; } +/* hero casts a spell of type spell_otyp, eg. SPE_SLEEP. + hero must know the spell. */ int -spelleffects(int spell, boolean atme) +spelleffects(int spell_otyp, boolean atme) { + int spell = spell_idx(spell_otyp); int energy, damage, chance, n, intell; int otyp, skill, role_skill, res = ECMD_OK; boolean confused = (Confusion != 0); @@ -915,13 +916,13 @@ spelleffects(int spell, boolean atme) * (There's no duplication of messages; when the rejection takes * place in getspell(), we don't get called.) */ - if ((spell < 0) || rejectcasting()) { + if ((spell == UNKNOWN_SPELL) || rejectcasting()) { return ECMD_OK; /* no time elapses */ } /* * Note: dotele() also calculates energy use and checks nutrition - * and strength requirements; it any of these change, update it too. + * and strength requirements; if any of these change, update it too. */ energy = (spellev(spell) * 5); /* 5 <= energy <= 35 */ @@ -1656,6 +1657,9 @@ dovspell(void) DISABLE_WARNING_FORMAT_NONLITERAL +/* shows menu of known spells, with options to sort them. + return FALSE on cancel, TRUE otherwise. + spell_no is set to the internal spl_book index, if any selected */ static boolean dospellmenu( const char *prompt, @@ -1940,7 +1944,7 @@ known_spell(short otyp) return FALSE; } -/* return index for spell otyp, or -1 if not found */ +/* return index for spell otyp, or UNKNOWN_SPELL if not found */ int spell_idx(short otyp) { @@ -1949,7 +1953,7 @@ spell_idx(short otyp) for (i = 0; (i < MAXSPELL) && (spellid(i) != NO_SPELL); i++) if (spellid(i) == otyp) return i; - return -1; + return UNKNOWN_SPELL; } /* forcibly learn spell otyp, if possible */ diff --git a/src/teleport.c b/src/teleport.c index a01a09fcb..77d16c15a 100644 --- a/src/teleport.c +++ b/src/teleport.c @@ -763,7 +763,7 @@ dotele( if (castit) { /* energy cost is deducted in spelleffects() */ exercise(A_WIS, TRUE); - if ((spelleffects(spell_idx(SPE_TELEPORT_AWAY), TRUE) & ECMD_TIME)) + if ((spelleffects(SPE_TELEPORT_AWAY, TRUE) & ECMD_TIME)) return 1; else if (!break_the_rules) return 0;