Clean up some spell-related code

Add two helper functions and use those outside of spell.c,
instead of iterating through all the spells.
This commit is contained in:
Pasi Kallinen
2022-02-20 21:12:26 +02:00
parent 041a07468a
commit 038ae7f984
5 changed files with 42 additions and 44 deletions
+2
View File
@@ -2536,6 +2536,8 @@ extern int tport_spell(int);
extern void losespells(void); extern void losespells(void);
extern int dovspell(void); extern int dovspell(void);
extern void initialspell(struct obj *); extern void initialspell(struct obj *);
extern boolean known_spell(short);
extern int spell_idx(short);
/* ### steal.c ### */ /* ### steal.c ### */
+2 -9
View File
@@ -1761,15 +1761,8 @@ jump(int magic) /* 0=Physical, otherwise skill level */
coord cc; coord cc;
/* attempt "jumping" spell if hero has no innate jumping ability */ /* attempt "jumping" spell if hero has no innate jumping ability */
if (!magic && !Jumping) { if (!magic && !Jumping && known_spell(SPE_JUMPING))
int sp_no; return spelleffects(spell_idx(SPE_JUMPING), FALSE);
for (sp_no = 0; sp_no < MAXSPELL; ++sp_no)
if (g.spl_book[sp_no].sp_id == NO_SPELL)
break;
else if (g.spl_book[sp_no].sp_id == SPE_JUMPING)
return spelleffects(sp_no, FALSE);
}
if (!magic && (nolimbs(g.youmonst.data) || slithy(g.youmonst.data))) { if (!magic && (nolimbs(g.youmonst.data) || slithy(g.youmonst.data))) {
/* normally (nolimbs || slithy) implies !Jumping, /* normally (nolimbs || slithy) implies !Jumping,
+7 -27
View File
@@ -760,7 +760,6 @@ gcrownu(void)
struct obj *obj; struct obj *obj;
boolean already_exists, in_hand; boolean already_exists, in_hand;
short class_gift; short class_gift;
int sp_no;
#define ok_wep(o) ((o) && ((o)->oclass == WEAPON_CLASS || is_weptool(o))) #define ok_wep(o) ((o) && ((o)->oclass == WEAPON_CLASS || is_weptool(o)))
HSee_invisible |= FROMOUTSIDE; HSee_invisible |= FROMOUTSIDE;
@@ -829,12 +828,8 @@ gcrownu(void)
u.ugifts++; u.ugifts++;
/* when getting a new book for known spell, enhance /* when getting a new book for known spell, enhance
currently wielded weapon rather than the book */ currently wielded weapon rather than the book */
for (sp_no = 0; sp_no < MAXSPELL; sp_no++) if (known_spell(class_gift) && ok_wep(uwep))
if (g.spl_book[sp_no].sp_id == class_gift) { obj = uwep; /* to be blessed,&c */
if (ok_wep(uwep))
obj = uwep; /* to be blessed,&c */
break;
}
} }
switch (u.ualign.type) { switch (u.ualign.type) {
@@ -1189,17 +1184,14 @@ pleased(aligntyp g_align)
/*FALLTHRU*/ /*FALLTHRU*/
case 6: { case 6: {
struct obj *otmp; struct obj *otmp;
int sp_no, trycnt = u.ulevel + 1; int trycnt = u.ulevel + 1;
/* not yet known spells given preference over already known ones; /* not yet known spells given preference over already known ones;
also, try to grant a spell for which there is a skill slot */ also, try to grant a spell for which there is a skill slot */
otmp = mkobj(SPBOOK_no_NOVEL, TRUE); otmp = mkobj(SPBOOK_no_NOVEL, TRUE);
while (--trycnt > 0) { while (--trycnt > 0) {
if (otmp->otyp != SPE_BLANK_PAPER) { if (otmp->otyp != SPE_BLANK_PAPER) {
for (sp_no = 0; sp_no < MAXSPELL; sp_no++) if (!known_spell(otmp->otyp)
if (g.spl_book[sp_no].sp_id == otmp->otyp)
break;
if (sp_no == MAXSPELL
&& !P_RESTRICTED(spell_skilltype(otmp->otyp))) && !P_RESTRICTED(spell_skilltype(otmp->otyp)))
break; /* usable, but not yet known */ break; /* usable, but not yet known */
} else { } else {
@@ -2007,21 +1999,9 @@ doturn(void)
int once, range, xlev; int once, range, xlev;
if (!Role_if(PM_CLERIC) && !Role_if(PM_KNIGHT)) { if (!Role_if(PM_CLERIC) && !Role_if(PM_KNIGHT)) {
/* Try to use the "turn undead" spell. /* Try to use the "turn undead" spell. */
* if (known_spell(SPE_TURN_UNDEAD))
* This used to be based on whether hero knows the name of the return spelleffects(spell_idx(SPE_TURN_UNDEAD), FALSE);
* turn undead spellbook, but it's possible to know--and be able
* to cast--the spell while having lost the book ID to amnesia.
* (It also used to tell spelleffects() to cast at self?)
*/
int sp_no;
for (sp_no = 0; sp_no < MAXSPELL; ++sp_no) {
if (g.spl_book[sp_no].sp_id == NO_SPELL)
break;
else if (g.spl_book[sp_no].sp_id == SPE_TURN_UNDEAD)
return spelleffects(sp_no, FALSE);
}
You("don't know how to turn undead!"); You("don't know how to turn undead!");
return ECMD_OK; return ECMD_OK;
} }
+25 -1
View File
@@ -915,7 +915,7 @@ spelleffects(int spell, boolean atme)
* (There's no duplication of messages; when the rejection takes * (There's no duplication of messages; when the rejection takes
* place in getspell(), we don't get called.) * place in getspell(), we don't get called.)
*/ */
if (rejectcasting()) { if ((spell < 0) || rejectcasting()) {
return ECMD_OK; /* no time elapses */ return ECMD_OK; /* no time elapses */
} }
@@ -1923,4 +1923,28 @@ initialspell(struct obj* obj)
return; return;
} }
/* return TRUE if hero knows spell otyp, FALSE otherwise */
boolean
known_spell(short otyp)
{
int i;
for (i = 0; (i < MAXSPELL) && (spellid(i) != NO_SPELL); i++)
if (spellid(i) == otyp)
return TRUE;
return FALSE;
}
/* return index for spell otyp, or -1 if not found */
int
spell_idx(short otyp)
{
int i;
for (i = 0; (i < MAXSPELL) && (spellid(i) != NO_SPELL); i++)
if (spellid(i) == otyp)
return i;
return -1;
}
/*spell.c*/ /*spell.c*/
+6 -7
View File
@@ -701,19 +701,18 @@ dotele(
} }
if (!trap && !break_the_rules) { if (!trap && !break_the_rules) {
boolean castit = FALSE; boolean castit = FALSE;
register int sp_no = 0, energy = 0; int energy = 0;
if (!Teleportation || (u.ulevel < (Role_if(PM_WIZARD) ? 8 : 12) if (!Teleportation || (u.ulevel < (Role_if(PM_WIZARD) ? 8 : 12)
&& !can_teleport(g.youmonst.data))) { && !can_teleport(g.youmonst.data))) {
/* Try to use teleport away spell. */ /* Try to use teleport away spell. */
for (sp_no = 0; sp_no < MAXSPELL; sp_no++) boolean knownsp = known_spell(SPE_TELEPORT_AWAY);
if (g.spl_book[sp_no].sp_id == SPE_TELEPORT_AWAY)
break;
/* casting isn't inhibited by being Stunned (...it ought to be) */ /* casting isn't inhibited by being Stunned (...it ought to be) */
castit = (sp_no < MAXSPELL && !Confusion); castit = (knownsp && !Confusion);
if (!castit && !break_the_rules) { if (!castit && !break_the_rules) {
You("%s.", You("%s.",
!Teleportation ? ((sp_no < MAXSPELL) !Teleportation ? (knownsp
? "can't cast that spell" ? "can't cast that spell"
: "don't know that spell") : "don't know that spell")
: "are not able to teleport at will"); : "are not able to teleport at will");
@@ -764,7 +763,7 @@ dotele(
if (castit) { if (castit) {
/* energy cost is deducted in spelleffects() */ /* energy cost is deducted in spelleffects() */
exercise(A_WIS, TRUE); exercise(A_WIS, TRUE);
if ((spelleffects(sp_no, TRUE) & ECMD_TIME)) if ((spelleffects(spell_idx(SPE_TELEPORT_AWAY), TRUE) & ECMD_TIME))
return 1; return 1;
else if (!break_the_rules) else if (!break_the_rules)
return 0; return 0;