src/weapon: Avoid touch_petrifies with invalid corpsenm

Filter out unset (-1) corpsenm before calling touch_petrifies.
While here, cleanup the oselect by doing excluding loop
and use can_touch_safely for filtering (suggested by entrez).
This commit is contained in:
Mika Kuoppala
2023-12-13 19:22:32 +02:00
committed by PatR
parent 83fba62152
commit 4669676fc0

View File

@@ -469,17 +469,24 @@ static struct obj *oselect(struct monst *, int);
} while (0)
static struct obj *
oselect(struct monst *mtmp, int x)
oselect(struct monst *mtmp, int type)
{
struct obj *otmp;
for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj) {
if (otmp->otyp == x
/* never select non-cockatrice corpses */
&& !((x == CORPSE || x == EGG)
&& !touch_petrifies(&mons[otmp->corpsenm]))
&& (!otmp->oartifact || touch_artifact(otmp, mtmp)))
return otmp;
if (otmp->otyp != type)
continue;
/* never select non-cockatrice corpses */
if ((type == CORPSE || type == EGG)
&& (otmp->corpsenm == NON_PM
|| !touch_petrifies(&mons[otmp->corpsenm])))
continue;
if (!can_touch_safely(mtmp, otmp))
continue;
return otmp;
}
return (struct obj *) 0;
}