ID:1884707
 
(See the best response by Ter13.)
If I'm trying to find the Area of a mob, is mob.loc.loc the only way to get it? Or is there a better way because mob.loc.loc is not always the area.
I don't understand why would mob.loc.loc not be the area unless mob has no loc.

If mob enters another a obj/mob then it'd be mob.loc.loc.loc.

It's either mob is inside a mob/obj, it's on a turf or it has no location. It's pretty finite and you can always use isturf(mob.loc) to check if it's on a turf or not.
I could run something like;

mob
proc
getArea(var/mob/Mob)
var/area
if(isturf(Mob.loc))
area = Mob.loc.loc
else if(isturf(Mob.loc.loc))
area = Mob.loc.loc.loc

return area


It crossed my mind, but I was wondering if there is an easier way to do it all with hard coded stuff.
Not that I know of.
This should do it:
atom/proc/GetArea()
area/GetArea() return src
turf/GetArea() return loc
atom/movable/GetArea()
if(!loc) return
var area/a = loc.loc
while(a && !istype(a)) a = a.loc
return a

edit: applied Lummox's fix.

I didn't realize contained objects inherited the coordinates of the containers, so Ter's method would be best. I mean, it makes sense, since movables on the map have the same coordinates as the turf they're on (but turfs don't inherit coordinates from their area).
Best response
Actually, there's a more reliable way to do this:

atom/movable/proc/get_area()
if(loc)
if(isarea(loc)) return loc
else
var/turf/t = locate(x,y,z)
if(t) return t.loc
else return null
else return null


This will get the area a movable is in in all cases.
In response to Kaiochao
If you use while(!istype(a)) you'll run into an error in some cases. If for instance the atom you're checking happens to be nowhere, or inside of another atom that's nowhere, then eventually a will be null. istype() will return 0 for that case, and then a=a.loc will attempt to access null.loc.

The correct loop would be:

while(a && !istype(a)) a = a.loc