ID:195094
 
//  Title: Improvised Movement
// Credit to: Hiead
// Contributed by: Hiead


/*
This snippet is an example of what I'll call "Improvised Movement."
The movable atom attempting movement will try to move only in the direction
of NORTH or SOUTH if told to move diagonally, and if it is unable to do so
it will then try to move in the other direction specified (NORTHEAST would
try NORTH, and then EAST). If movement still fails, the atom will try to
move in the original specified direction (NORTHEAST would try NORTH, then
upon failure it would try EAST, and if that failed it would try NORTHEAST).

This can be useful when writing AI if you don't want to necessarily come
up with your own path-finding routines, since the built-in procs for
finding a path often return diagonal directions. From that, one might simply
overwrite the Move() proc to prevent the character from moving diagonally,
or even possibly just to simply have them move NORTH or SOUTH if a diagonal
is called for, but if their movement fails there and the only path to some
object is in a diagonal direction, they might often be found in an infinite
loop.
*/


atom/movable/Move(newLoc, newDir)
if(!newDir) // If being simply relocated, treat it normally
. = ..()

else
if(newDir & NORTH)
if(newDir != NORTH) // If newDir is diagonal
. = step(src, NORTH)
if(!.)
. = step(src, newDir ^ NORTH) // Try without NORTH
if(!.)
. = ..() // Try original direction
else . = ..()

else if(newDir & SOUTH)
if(newDir != SOUTH)
. = step(src, SOUTH)
if(!.)
. = step(src, newDir ^ SOUTH)
if(!.)
. = ..()
else . = ..()

else . = ..()


Hiead