// Title: Closest of Type
// Credit to: Hiead
// Contributed by: Hiead
/*
This snippet shows a procedure that returns a list of the closest
objects of a specified type. It starts off by determining the distance
from the central object to the nearest type specified, and then it loops
through a list of atoms in that distance and pulls out atoms of the
specified type.
The first two parameters resemble those of several built-in DM
procs: maxDistance is the maximum distance from which a "closest" atom
may be included, and center holds a reference to the object from which
the distances to other objects must be compared.
The third parameter is the cornerstone of the whole proc; it specifies
the type of which the "closest" objects must be. The fourth parameter is
just a specifier as to whether the central object itself should be included
in the filter. If true, it should be noted that the closest range will
then be 0, and only objects sharing the same location and of the specified
type will be returned.
*/
proc
Closest(maxDistance=world.view, center=usr, checkType=/atom, useSelf=FALSE)
if(!isnum(maxDistance) || !maxDistance)
maxDistance = 5
. = new/list // Will return a list
var/closest = maxDistance
var/list/surrounding
if(useSelf) // Include center in test?
surrounding = range(closest, center)
else
surrounding = range(closest, center) - center
for(var/atom in surrounding)
if(istype(atom, checkType))
var/dist = get_dist(center, atom)
if(dist < closest)
closest = dist
if(useSelf) // Include center in return?
surrounding = range(closest, center)
else
surrounding = range(closest, center) - center
for(var/atom in surrounding)
if(istype(atom, checkType))
. += atom
///*
// Testing Code/Sample Implementation:
world
maxx = 13
maxy = 13
mob/Login()
..()
loc = locate(7, 7, 1)
mob/verb
CreateMob()
var/global/count = 0
var/mob/M = new(locate(rand(1, world.maxx), rand(1, world.maxy), 1))
M.name = "Mob #[++count]"
src << "[M.name] created at ([M.x],[M.y])"
ClosestMobs()
var/list/closestMobs = Closest(6, src, /mob)
src << "<hr>"
if(!closestMobs.len)
src << "No mobs are nearby."
else for(var/mob/M in closestMobs)
src << "[M.name] @ distance = [get_dist(src, M)]"
src << "<hr>"
//*/
Hiead