ID:139600
 
Code:
proc
UNDOMOVE(direction)
var/rdir=0
switch(direction)
if(NORTH) rdir=SOUTH
if(SOUTH) rdir=NORTH
if(EAST) rdir=WEST
if(WEST) rdir=EAST
if(NORTHEAST) rdir=SOUTHWEST
if(NORTHWEST) rdir=SOUTHEAST
if(SOUTHWEST) rdir=NORTHEAST
if(SOUTHEAST) rdir=NORTHWEST
return rdir


proc
recordmove(mob/M)
var/list/recorded_moves=new/list();var/canmove=rand(5,15);var/counter=0
var/maxcount=canmove;var/list/CHOSEN=list(NORTH, SOUTH, EAST, WEST, NORTHEAST, NORTHWEST, SOUTHEAST, SOUTHWEST)
var/END=0
while(M)
if(counter<maxcount)
counter++
var/picker=pick(CHOSEN)
recorded_moves.Add(picker)
step(M,picker)
else
END=maxcount*2
counter++
var/UNDOR=recorded_moves[recorded_moves.len]
step(M,UNDOMOVE(UNDOR))
if(counter>=END)
//world<<"should be back at home location"
counter=0
canmove=rand(5,15)
maxcount=canmove
sleep(1)//rand(20,135))


Problem description:
Not working correctly the npc is just all over the place, i've been messing with this for awhile and can't figure it out... may be its right in front of my face or to advanced. Thank you

UNDOMOVE() can just be shortened to turn(dir,180).

Don't place a whole bunch of variable declarations on the same line using semicolons. It just makes things harder to read with no benefit. It's only reasonable if you're doing something like var/x,y,z, and not initializing them (unless to the same value).

new/list() is not the proper way to create a new list. Use the list() proc instead.

The structure of the while() loop is really quite strange. You should have two loops: one to make random steps, and one to undo those steps. They should additionally be for() loops: for(var/v = 1 to canmove), and for(var/v = canmove to 1 step -1). Alternative, for(var/v = 1, v <= canmove, ++v) and for(var/v = canmove, v >= 1, --v).

You are always just reading the last element of recorded_moves, so you just keep on taking the same backwards step until you take new random steps. Obviously, it's highly unlikely that this will take you to your original location.
In response to Garthor
thank you, fixed my problems and shorted the code.. appreciate your time to help me fix this problem