]> granicus.if.org Git - nethack/commitdiff
Some spell code reorg
authorPasi Kallinen <paxed@alt.org>
Wed, 23 Feb 2022 08:45:14 +0000 (10:45 +0200)
committerPasi Kallinen <paxed@alt.org>
Wed, 23 Feb 2022 08:52:10 +0000 (10:52 +0200)
Keep the internal spell array index inside spell.c,
and refer to spells outside of it with the otyp id.

include/spell.h
src/apply.c
src/pray.c
src/spell.c
src/teleport.c

index 47928345883df04d2167a61a112e951ac9e5cdb1..4090850b9ac9be82de9688fa608a1302158b3567 100644 (file)
@@ -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
index ef73be941b47d2cd676cb16d924bbfbfc01e180b..9d16053a7e4327d455c85cd4990f21d257a9b04e 100644 (file)
@@ -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,
index 11557abb6c8016fca08c6a001ceeeb0e3f519601..d12b848cb437ca2e4cd95ff481f00361fe76210e 100644 (file)
@@ -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;
     }
index 2b2a0292c4084172d64a05cdf9fc5934937d2fe7..76de4c57d3ee0aedb32d9e782fd2c367f8585231 100644 (file)
@@ -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 */
index a01a09fcbe53e9bf8b9867a758f43d357d8058eb..77d16c15a02cbfd013bbc02440dcafd4596ac063 100644 (file)
@@ -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;