//if we don't have to wait, we're home free
if (delay) {
//get remaining piston time
def timeLeft = 20000 + rtData.timestamp - now()
//negative delays force us to reschedule, no sleeping on this one
boolean reschedule = (delay < 0)
delay = reschedule ? -delay : delay
//we're aiming at waking up with at least 10s left
if (reschedule || (timeLeft - delay < 10000) || (delay >= 5000) || async) {
//schedule a wake up
if (rtData.logging > 1) trace "Requesting a wake up for ${formatLocalTime(now() + delay)} (in ${cast(rtData, delay / 1000, 'decimal')}s)", rtData
tracePoint(rtData, "t:${task.$}", now() - t, -delay)
requestWakeUp(rtData, statement, task, delay, task.c)
return false
} else {
if (rtData.logging > 1) trace "Waiting for ${delay}ms", rtData
pause(delay)
}
}
tracePoint(rtData, "t:${task.$}", now() - t, delay)
return true
It depends on a few factors in this line.
if (reschedule || (timeLeft - delay < 10000) || (delay >= 5000) || async)
- Waits greater than or equal to 5 seconds cause a wakeup(delay >= 5000)
- Async Statements (async) will always request a wakeup
timeleft - delay < 10000
- If I’m remembering the above correctly, the piston has 20 seconds to do whatever it needs to do. It will reschedule a wakeup if the delay requested will prevent it from having 10 seconds left to do whatever it needs to do after.
So if the piston had been executing for 7 seconds up to this point. It would have 13 left to do the rest of the items (20 - 7 = 13 = timeleft). If you request a wait time for 3 seconds it would put your right at the 10 second mark which pause without requesting a wakeup (13 - 3 = 10 >= 10 ). Waiting for 4 seconds however would put the time left after the pause at 9 seconds which is passed the threshold causing a wakeup.
reschedule
- Not too sure about this one. It’s if the delay is negative and it will take the absolute value instead but I haven’t debugged to see where the delay can be negative.
I’m more surprised though that it’s re-evaluating conditions for you. From what I’ve seen in the code it’s supposed to fast forward to the block you came from and not check the conditions again but that’s only for code it’s already run though. Are the conditions you noticed changing after the wait command? Would it be possible to save the states you want to check in variables before the wait?