ID:195151
 
//Title: Improved General Direction Proc
//Credit to: Lummox JR
//Contributed by: Jtgibson

/*
Well, what can I say... Lummox JR is to me what European chocolate is to
American candy bars -- better. Lummox's improvement of my General Direction
Proc (q.v.) is faster, smaller, and lasts for over 30 loads of laundry. Of
course, you probably shouldn't try to wrap your head around it until you've at
least gone through high-school computer science. I even had to add whitespace
to make it human readable!

If you want to see what kind of results this spits out, check out the lengthy
explanation in the General Direction Proc's description. Lummox's does the same
thing.

--Jtgibson
*/


proc/get_general_dir(atom/ref,atom/target)
if(target.z > ref.z) return UP
if(target.z < ref.z) return DOWN

var/d = get_dir(ref,target)
if(d&d-1) // diagonal
var/ax = abs(ref.x - target.x)
var/ay = abs(ref.y - target.y)
if(ax >= (ay << 1)) return d & (EAST|WEST) // keep east/west (4 and 8)
else if(ay >= (ax << 1)) return d & (NORTH|SOUTH) // keep north/south (1 and 2)
return d