proc/weigh_dirs(d1, d2)
if(d2 == turn(d1, 180)) // difficult scenario, so just randomly choose clockwise or counterclockwise
return turn(d1, pick(45,-45))
if(d1 == d2) return d2
switch(d1)
if(WEST)
if(d2 & SOUTH)
return SOUTHWEST
if(d2 & NORTH)
return NORTHWEST
if(SOUTH)
if(d2 & WEST)
return SOUTHWEST
if(d2 & EAST)
return SOUTHEAST
if(EAST)
if(d2 & SOUTH)
return SOUTHEAST
if(d2 & NORTH)
return NORTHEAST
if(NORTH)
if(d2 & WEST)
return NORTHWEST
if(d2 & EAST)
return NORTHEAST
if(SOUTHWEST)
if(d2 & SOUTH)
return SOUTH
if(d2 & NORTH)
return WEST
if(d2 & WEST)
return WEST
if(d2 & EAST)
return SOUTH
if(NORTHWEST)
if(d2 & SOUTH)
return WEST
if(d2 & NORTH)
return NORTH
if(d2 & WEST)
return WEST
if(d2 & EAST)
return NORTH
if(SOUTHEAST)
if(d2 & SOUTH)
return SOUTH
if(d2 & NORTH)
return EAST
if(d2 & WEST)
return SOUTH
if(d2 & EAST)
return EAST
if(NORTHEAST)
if(d2 & SOUTH)
return EAST
if(d2 & NORTH)
return NORTH
if(d2 & WEST)
return NORTH
if(d2 & EAST)
return EAST
return d2
For example, if d1 = NORTH and d2 = WEST, the return would be NORTHWEST. This is essentially d1 | d2, yes, but what about a more complicated scenario?
What about d1 = NORTHEAST, d2 = WEST? This would return NORTH as NORTH is closest "turn" toward the desired direction. I use this bloated function to simulate a sort of "semi-homing" projectile turn:
The above code works, yes, but it's probably not the most elegant method of going about doing this. Is there some clever alternative to what I am doing other than defining almost every situation possible?
(Time to over-complicate!)
The method I've used to determine which way to turn is to just get the angle difference between the directions. I think there's a fairly recent thread about getting the angle between directions, actually, but whatever.
This here is code I just wrote off the top of my head, going at the problem from a slightly different angle.