ID:195040
 
//Title: Movement-locking system.
//Credit to: Popisfizzy

/*
I've found that this system has come up several times on
the developer forums, so I thought I'd post it here. All
it is a simple locking and delay mechanism, to (wait for
it) lock and delay movement.
*/


mob
var
locked = 0 //If true, the player can't move.
//Otherwise, they can.
delay = 0 //This is the amount of ticks until the
//player can move again.

Move()
if(locked)
//If the locked var is true, indicate failure.
return 0

if(delay > 0)
//If they have a delay, call the parent proc,
//and store its return value.
. = ..()

if(.)
//If they are able to move, then lock them
//for the delay period.
Lock(delay)

return . //This will allow movement, but not
//allow them to move again until they
//are unlocked.

return ..() //If not, pass the parent proc.

proc
Delay(d = 0)
delay = d //Set the delay.
return 1 //Indicate success.

Lock(d)
//If a negative number is passed as the delay,
//the mob will be locked indefinitely. That is,
//until Unlock() is called.

//If d isn't set, default to delay.
d = d || delay

if(locked)
//If the mob is already locked, indicate
//that the proc failed to lock them.
return 0

locked = 1 //Lock the mob.
if(d > 0)
//If the delay is greater than zero, wait
//that amount of time, then unlock them.
spawn(d)
Unlock()

return 1 //Indicate that they were locked
//successfully.

Unlock()
if(!locked)
//If they are unlocked, indicate that the
//proc failed to unlock them.
return 0

locked = 0
return 1 //Indicate they were successfully
//unlocked.