ID:2139557
 
(See the best response by Ter13.)
Code:
mob/proc/
DO_MOVEMENT()
set background=1
if(Moving)return;Moving=1
var/FirstStep=1
if(super_speed)FirstStep = 0
while(MN || ME || MW || MS || QueN || QueS || QueE || QueW)
if(!speed_run)
if(run) icon_state="Run"
if(MN || QueN)
if(ME || QueE) if(!step(src,NORTHEAST) && !step(src,NORTH)) step(src,EAST)
else if(MW || QueW) if(!step(src,NORTHWEST) && !step(src,NORTH)) step(src,WEST)
else step(src,NORTH)
else if(MS || QueS)
if(ME || QueE) if(!step(src,SOUTHEAST) && !step(src,SOUTH)) step(src,EAST)
else if(MW || QueW) if(!step(src,SOUTHWEST) && !step(src,SOUTH)) step(src,WEST)
else step(src,SOUTH)
else if(ME || QueE) step(src,EAST)
else if(MW || QueW) step(src,WEST)
QueN=0;QueS=0;QueE=0;QueW=0
if(FirstStep) {sleep(1);FirstStep=0}
sleep(GetMovementSpeed())

Moving=0
icon_state = ""

mob/verb
MoveNorth()
set hidden=1;set instant=1;set background = 1
src.SprintCheck("North")
src.MN=1;src.MS=0;QueN=1;src.MovementLoop()


Problem description:

Any ideas of how I can reduce cpu usage for this movement loop?
Best response
mob/var/tmp
move_dir = 0
move_keys = 0
move_loop

proc
MoveLoop()
set waitfor = 0
if(move_loop!=null) return
move_loop = world.time
var/diag, steps = super_speed ? 1 : 0
while(move_dir)
if(!speed_run)
diag = move_dir&move_dir-1
icon_state = run ? "Run" : null //best to do this elsewhere.
if(!step(src,move_dir)&&diag)
step(src,diag))||step(src,move_dir-diag)
if(steps++)
sleep(1+GetMovementSpeed())
else
sleep(GetMovementSpeed())
move_loop = null
icon_state = null

verb
MoveKey(dir as num,state as num) //replace all of your MoveNorth, etc verbs with this single verb. state is 0 or 1 for release or press. dir is 1 for NORTH, 2 for SOUTH, 4 for EAST, 8 for WEST.
set hidden = 1
set instant = 1
var/opp = turn(dir,180), okey = move_keys
if(state)
move_keys |= dir
move_dir |= dir
if(move_dir&opp)
move_dir &= ~opp
else
move_keys &= ~dir
move_dir &= ~dir
if(move_keys&opp)
move_dir |= opp
if(!okey&&move_keys)
spawn(0) //this is to allow pending verbs to fire before the move loop executes
MoveLoop()


TL;DR: Don't use 8 variables to keep track of movement controls. There's no need for more than two variables to keep track of the key state and movement direction.

Also, you are using a lot of booleans where arithmetic operators would work to prevent lookups and if-statements.

You also shouldn't be setting any of these things to the background. That's a symptom of some pretty serious problems in the rest of your code.
Why is it that I can't take normal steps like north south east west.
In response to Mav472
Because math.
Sorry, flipped something in the if statement. Try it now.
Great job. Thanks a ton. One question though, what type of procs should and should not be set to the background setting (I've already read the guide)?
Never set any proc as background ever
Never set any proc as background ever

Basically this, it is now more favorable to check tick_completion to prevent new player actions from triggering further behavior when a tick is in overrun. background was never very good, and as such has been unofficially deprecated in favor of newer, more controllable tools.