From 878ac240378f968b55e60cf87bd4f015f9de6197 Mon Sep 17 00:00:00 2001 From: Pasi Kallinen Date: Sat, 17 Nov 2018 19:26:49 +0200 Subject: [PATCH] Fix freeing monsters not on map Sometimes we free the monster data, but the monster is not on the map - usually this happens if the map is full of monsters and a new one is migrated on the level. Make m_detach check the monster x coordinate, so it knows not to touch the map if the monster isn't on it. --- src/dog.c | 1 + src/mail.c | 3 ++- src/mon.c | 15 ++++++++++++--- 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/dog.c b/src/dog.c index e5767103a..71a4ea289 100644 --- a/src/dog.c +++ b/src/dog.c @@ -464,6 +464,7 @@ fail_mon_placement: } (void) mkcorpstat(CORPSE, (struct monst *) 0, mtmp->data, xlocale, ylocale, CORPSTAT_NONE); + mtmp->mx = mtmp->my = -1; /* for mongone, mon is not anywhere */ mongone(mtmp); } } diff --git a/src/mail.c b/src/mail.c index 3f47e3a55..6427cf8cd 100644 --- a/src/mail.c +++ b/src/mail.c @@ -418,7 +418,8 @@ struct mail_info *info; /* zip back to starting location */ go_back: - (void) md_rush(md, start.x, start.y); + if (!md_rush(md, start.x, start.y)) + md->mx = md->my = -1; /* for mongone, md is not on map */ mongone(md); /* deliver some classes of messages even if no daemon ever shows up */ give_up: diff --git a/src/mon.c b/src/mon.c index 079321f41..7340e1ad6 100644 --- a/src/mon.c +++ b/src/mon.c @@ -1745,6 +1745,8 @@ m_detach(mtmp, mptr) struct monst *mtmp; struct permonst *mptr; /* reflects mtmp->data _prior_ to mtmp's death */ { + boolean onmap = (mtmp->mx > -1); + if (mtmp == context.polearm.hitmon) context.polearm.hitmon = 0; if (mtmp->mleashed) @@ -1753,14 +1755,21 @@ struct permonst *mptr; /* reflects mtmp->data _prior_ to mtmp's death */ mtmp->mtrapped = 0; mtmp->mhp = 0; /* simplify some tests: force mhp to 0 */ relobj(mtmp, 0, FALSE); - remove_monster(mtmp->mx, mtmp->my); + if (onmap) { + if (mtmp->wormno) + remove_worm(mtmp); + else + remove_monster(mtmp->mx, mtmp->my); + } if (emits_light(mptr)) del_light_source(LS_MONSTER, monst_to_any(mtmp)); if (mtmp->m_ap_type) seemimic(mtmp); - newsym(mtmp->mx, mtmp->my); + if (onmap) + newsym(mtmp->mx, mtmp->my); unstuck(mtmp); - fill_pit(mtmp->mx, mtmp->my); + if (onmap) + fill_pit(mtmp->mx, mtmp->my); if (mtmp->isshk) shkgone(mtmp); -- 2.40.0