ID:1469502
 
    BoundAlign(var/atom/movable/Ref, var/atom/movable/Trg, var/x_dist = 0, var/y_dist = 0)
/* This function should align Ref to Target. x_dist is the number of pixels in the x-direction from the bottom of
Trg's bounding box you want to move Ref to. y_dist is the same thing for y. By default, this code should try to
move Ref to align with Trg's bottom left corner.*/


var/xDiff = ((Ref.x * 32) + (Ref.step_x)) - ((Trg.x * 32) + (Trg.step_x))
var/yDiff = ((Ref.y * 32) + (Ref.step_y)) - ((Trg.y * 32) + (Trg.step_y))
while(xDiff != x_dist)
if(xDiff < x_dist)
if(step(Ref, EAST, 1))
xDiff = ((Ref.x * 32) + (Ref.step_x)) - ((Trg.x * 32) + (Trg.step_x))
else break
else if(xDiff > x_dist)
if(step(Ref, WEST, 1))
xDiff = ((Ref.x * 32) + (Ref.step_x)) - ((Trg.x * 32) + (Trg.step_x))
else break
while(yDiff != y_dist)
if(yDiff > y_dist)
if(step(Ref, SOUTH, 1))
yDiff = ((Ref.y * 32) + (Ref.step_y)) - ((Trg.y * 32) + (Trg.step_y))
else break
else if(yDiff < y_dist)
if(step(Ref, NORTH, 1))
yDiff = ((Ref.y * 32) + (Ref.step_y)) - ((Trg.y * 32) + (Trg.step_y))
else break


This is designed for pixel movement, and I don't know what's lying around in snippets, so I'll just leave this here. I tend to use this with objects of zero density if I can. The reason being, it moves the objects east/west first, then north/south. This is incredibly weak functionality, and without a stronger AI or the knowledge of one, it's the best I can do. I'm not going to devote a ton of time into making a perfect AI system so it aligns these bounds the best it can. The most modification I'll probably do to this is to add a priority variable, so it does 1 direction first based on priority.

One thing to keep in mind that if you're dealing with multi-tile icons/objects, be wary of the direction. The best method (I find) is to set it based on the SOUTH or NORTH direction and change direction afterwards, so it fits.