From 1f2d16082eb37fea785c6628e186244aaa991e0e Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Tue, 11 Jan 2022 22:55:40 +0200 Subject: [PATCH] Fix shopkeeper Kop summons causing segfault ... if it happened on a level with no downstairs, such as the tourist quest. --- include/extern.h | 1 + src/shk.c | 17 +++++++---------- src/wizard.c | 11 +++++------ 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/include/extern.h b/include/extern.h index 5178912c0..f890f7d84 100644 --- a/include/extern.h +++ b/include/extern.h @@ -3112,6 +3112,7 @@ extern void nethack_enter_windows(void); extern void amulet(void); extern int mon_has_amulet(struct monst *); extern int mon_has_special(struct monst *); +extern void choose_stairs(xchar *, xchar *, boolean); extern int tactics(struct monst *); extern boolean has_aggravatables(struct monst *); extern void aggravate(void); diff --git a/src/shk.c b/src/shk.c index 8125dde02..edcee5cea 100644 --- a/src/shk.c +++ b/src/shk.c @@ -344,14 +344,9 @@ call_kops(register struct monst* shkp, register boolean nearshop) { coord mm; - stairway *stway = g.stairs; + xchar sx = 0, sy = 0; - while (stway) { - if (!stway->isladder && !stway->up - && stway->tolev.dnum == u.uz.dnum) - break; - stway = stway->next; - } + choose_stairs(&sx, &sy, TRUE); if (nearshop) { /* Create swarm around you, if you merely "stepped out" */ @@ -365,9 +360,11 @@ call_kops(register struct monst* shkp, register boolean nearshop) if (flags.verbose) pline_The("Keystone Kops are after you!"); /* Create swarm near down staircase (hinders return to level) */ - mm.x = stway->sx; - mm.y = stway->sy; - makekops(&mm); + if (isok(sx, sy)) { + mm.x = sx; + mm.y = sy; + makekops(&mm); + } /* Create swarm near shopkeeper (hinders return to shop) */ mm.x = shkp->mx; mm.y = shkp->my; diff --git a/src/wizard.c b/src/wizard.c index 86f06174e..8271d9526 100644 --- a/src/wizard.c +++ b/src/wizard.c @@ -17,7 +17,6 @@ static struct obj *on_ground(short); static boolean you_have(int); static unsigned long target_on(int, struct monst *); static unsigned long strategy(struct monst *); -static void choose_stairs(xchar *, xchar *); /* adding more neutral creatures will tend to reduce the number of monsters summoned by nasty(); adding more lawful creatures will reduce the number @@ -311,12 +310,12 @@ strategy(struct monst *mtmp) return dstrat; } -static void -choose_stairs(xchar *sx, xchar *sy) +void +choose_stairs(xchar *sx, xchar *sy, boolean dir) { xchar x = 0, y = 0; stairway *stway = g.stairs; - boolean stdir = !builds_up(&u.uz); + boolean stdir = dir && !builds_up(&u.uz); if ((stway = stairway_find_type_dir(FALSE, stdir)) != 0) { x = stway->sx; @@ -335,7 +334,7 @@ choose_stairs(xchar *sx, xchar *sy) } } - if (x && y) { + if (isok(x, y)) { *sx = x; *sy = y; } @@ -356,7 +355,7 @@ tactics(struct monst *mtmp) case STRAT_HEAL: /* hide and recover */ mx = mtmp->mx, my = mtmp->my; /* if wounded, hole up on or near the stairs (to block them) */ - choose_stairs(&sx, &sy); + choose_stairs(&sx, &sy, FALSE); mtmp->mavenge = 1; /* covetous monsters attack while fleeing */ if (In_W_tower(mx, my, &u.uz) || (mtmp->iswiz && !sx && !mon_has_amulet(mtmp))) {