Split deadbook undead pacify effect into separate function

Also make the monster chain iterator functions more robust,
in case the called function changes the next monster pointer.
This commit is contained in:
Pasi Kallinen
2023-10-28 19:01:46 +03:00
parent 204cf6bf10
commit 638c487c59
2 changed files with 31 additions and 26 deletions
+9 -6
View File
@@ -4125,9 +4125,10 @@ iter_mons_safe(boolean (*func)(struct monst *))
void void
iter_mons(void (*func)(struct monst *)) iter_mons(void (*func)(struct monst *))
{ {
struct monst *mtmp; struct monst *mtmp, *mtmp2;
for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) { for (mtmp = fmon; mtmp; mtmp = mtmp2) {
mtmp2 = mtmp->nmon;
if (DEADMONSTER(mtmp) || mon_offmap(mtmp)) if (DEADMONSTER(mtmp) || mon_offmap(mtmp))
continue; continue;
func(mtmp); func(mtmp);
@@ -4140,9 +4141,10 @@ iter_mons(void (*func)(struct monst *))
struct monst * struct monst *
get_iter_mons(boolean (*func)(struct monst *)) get_iter_mons(boolean (*func)(struct monst *))
{ {
struct monst *mtmp; struct monst *mtmp, *mtmp2;
for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) { for (mtmp = fmon; mtmp; mtmp = mtmp2) {
mtmp2 = mtmp->nmon;
if (DEADMONSTER(mtmp) || mon_offmap(mtmp)) if (DEADMONSTER(mtmp) || mon_offmap(mtmp))
continue; continue;
if (func(mtmp)) if (func(mtmp))
@@ -4158,9 +4160,10 @@ struct monst *
get_iter_mons_xy(boolean (*func)(struct monst *, coordxy, coordxy), get_iter_mons_xy(boolean (*func)(struct monst *, coordxy, coordxy),
coordxy x, coordxy y) coordxy x, coordxy y)
{ {
struct monst *mtmp; struct monst *mtmp, *mtmp2;
for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) { for (mtmp = fmon; mtmp; mtmp = mtmp2) {
mtmp2 = mtmp->nmon;
if (DEADMONSTER(mtmp) || mon_offmap(mtmp)) if (DEADMONSTER(mtmp) || mon_offmap(mtmp))
continue; continue;
if (func(mtmp, x, y)) if (func(mtmp, x, y))
+22 -20
View File
@@ -28,6 +28,7 @@
static int spell_let_to_idx(char); static int spell_let_to_idx(char);
static boolean cursed_book(struct obj * bp); static boolean cursed_book(struct obj * bp);
static boolean confused_book(struct obj *); static boolean confused_book(struct obj *);
static void deadbook_pacify_undead(struct monst *);
static void deadbook(struct obj *); static void deadbook(struct obj *);
static int learn(void); static int learn(void);
static boolean rejectcasting(void); static boolean rejectcasting(void);
@@ -197,12 +198,31 @@ confused_book(struct obj* spellbook)
return gone; return gone;
} }
/* pacify or tame an undead monster */
static void
deadbook_pacify_undead(struct monst *mtmp)
{
if ((is_undead(mtmp->data) || is_vampshifter(mtmp))
&& cansee(mtmp->mx, mtmp->my)) {
mtmp->mpeaceful = TRUE;
if (sgn(mtmp->data->maligntyp) == sgn(u.ualign.type)
&& mdistu(mtmp) < 4)
if (mtmp->mtame) {
if (mtmp->mtame < 20)
mtmp->mtame++;
} else
(void) tamedog(mtmp, (struct obj *) 0);
else
monflee(mtmp, 0, FALSE, TRUE);
}
}
/* special effects for The Book of the Dead; reading it while blind is /* special effects for The Book of the Dead; reading it while blind is
allowed so that needs to be taken into account too */ allowed so that needs to be taken into account too */
static void static void
deadbook(struct obj* book2) deadbook(struct obj* book2)
{ {
struct monst *mtmp, *mtmp2; struct monst *mtmp;
coord mm; coord mm;
You("turn the pages of the Book of the Dead..."); You("turn the pages of the Book of the Dead...");
@@ -295,25 +315,7 @@ deadbook(struct obj* book2)
mm.y = u.uy; mm.y = u.uy;
mkundead(&mm, TRUE, NO_MINVENT); mkundead(&mm, TRUE, NO_MINVENT);
} else if (book2->blessed) { } else if (book2->blessed) {
for (mtmp = fmon; mtmp; mtmp = mtmp2) { iter_mons(deadbook_pacify_undead);
mtmp2 = mtmp->nmon; /* tamedog() changes chain */
if (DEADMONSTER(mtmp))
continue;
if ((is_undead(mtmp->data) || is_vampshifter(mtmp))
&& cansee(mtmp->mx, mtmp->my)) {
mtmp->mpeaceful = TRUE;
if (sgn(mtmp->data->maligntyp) == sgn(u.ualign.type)
&& mdistu(mtmp) < 4)
if (mtmp->mtame) {
if (mtmp->mtame < 20)
mtmp->mtame++;
} else
(void) tamedog(mtmp, (struct obj *) 0);
else
monflee(mtmp, 0, FALSE, TRUE);
}
}
} else { } else {
switch (rn2(3)) { switch (rn2(3)) {
case 0: case 0: