ID:157263
 
mob/verb
Fire()
var/vch=0
for(var/x,x<=5,x++)
for(var/mob/M in ohearers(x,src))
if(M)
vch++
break
if(vch) break
if(vch)
world<<"Mob found"
else
world<<"No mobs found"
src<<"End Check"


Is there a more efficient way of finding the closest (visible) mob ^-- like that, only without looping through each ohearers?
view() and related procs are already sorted by distance.
In response to Garthor
When around a large group of mob's it always want's to fire to the bottom-left most mob.
using:
mob/verb
Fire()
for(var/mob/M in ohearers(5,src))//won't fire through opaque walls
if(M)
missile(/obj/bullet, src, M)
break
In response to Leur
Whoops, guess viewers() and hearers() sort by x-y-z, rather than distance to center. Changing it to oview() or somesuch will work.
In response to Garthor
I've used ohearers, still uses as you say x-y-z sorting. Would be nice to get an option of sorting by distance without looping the code. Think I should suggest it or stick with the loops?

For anyone who wants it:
Demo (Hope I made it clear enough to follow with the comments.) : http://www.byond.com/developer/Leur/ClosestMob
Pretty simple to most people but *shrugs*.
In response to Leur
Again, ohearers does not sort by distance. If you want to sort by distance without having to do it yourself, use oview(), view(), orange(), or range().
In response to Garthor
oview / orange doesn't filter out the "not visible" like ohearers though.
http://www.byond.com/members/Leur/files/Env_src.zip <- highlighted area is what I mean it still fires through opaque atoms.