- for (mtmp = fmon; mtmp; mtmp = mtmp->nmon)
- if (mtmp && !DEADMONSTER(mtmp) && !mtmp->mtame
- && cansee(mtmp->mx, mtmp->my)
- && distu(mtmp->mx, mtmp->my) <= max_range
- && distu(mtmp->mx, mtmp->my) >= min_range) {
- if (selmon)
- return FALSE;
- selmon = mtmp;
+ if (Blind)
+ return FALSE; /* must be able to see target location */
+ impaired = (Confusion || Stunned || Hallucination);
+ mpos.x = mpos.y = 0; /* no candidate location yet */
+ rt = isqrt(max_range);
+ lo_x = max(u.ux - rt, 1), hi_x = min(u.ux + rt, COLNO - 1);
+ lo_y = max(u.uy - rt, 0), hi_y = min(u.uy + rt, ROWNO - 1);
+ for (x = lo_x; x <= hi_x; ++x) {
+ for (y = lo_y; y <= hi_y; ++y) {
+ if (distu(x, y) < min_range || distu(x, y) > max_range
+ || !isok(x, y) || !cansee(x, y))
+ continue;
+ glyph = glyph_at(x, y);
+ if (!impaired
+ && glyph_is_monster(glyph)
+ && (mtmp = m_at(x, y)) != 0
+ && (mtmp->mtame || (mtmp->mpeaceful && flags.confirm)))
+ continue;
+ if (glyph_is_monster(glyph)
+ || glyph_is_warning(glyph)
+ || glyph_is_invisible(glyph)
+ || (glyph_is_statue(glyph) && impaired)) {
+ if (mpos.x)
+ return FALSE; /* more than one candidate location */
+ mpos.x = x, mpos.y = y;
+ }