// devlog/003
The timer that skipped zero
A clock in Emberbound only changes when it hits exactly zero. Then the offline replay started moving time five seconds at a time, and stepped right over it.
Late in Emberbound there's a thing called the Turning. Every 30 seconds it switches to a new phase: Equinox, Eclipse, Nova, Conjunction or Zenith. Each phase makes some things stronger and stops others for a while. Nova, for example, stops your Dawn and Dusk. So it matters which phase you're in, and when the next one comes.
This post is about the 30 seconds in between, and how they once ran out and never came back.
Half a second at a time
While the game is open, it runs a big update twice a second. Each update takes half a second off the Turning's timer. When the timer reaches zero, the game picks a new phase and starts again from 30.
cooldown -= 0.5; // twice a second
if (cooldown === 0) {
cooldown = 30;
pickNewPhase();
}
Notice it checks for exactly zero, not "zero or less". That's fine here. Starting from 30 and taking off 0.5 each time, you can't miss it.
Then came the replay
Idle games are supposed to keep going while they're closed. When you open Emberbound again, it takes the time you were away and replays it, up to two hours. Replaying it half a second at a time would mean 14,400 updates before you see your gold, so the replay takes bigger steps: five seconds each, 1,440 of them.
For most of the game that doesn't matter. Gold, flame and everything else grow the same whether you add them up in small pieces or big ones. The Turning's timer is different, because it only cares about one exact number. A five-second step counts as ten updates, so it takes off 5 at once:
cooldown -= 0.5 * 10; // five seconds' worth
If you closed the game with 25 seconds on the clock, the replay went 20, 15, 10, 5, 0, and the phase changed like normal. But if you closed it with 27.5, it went 22.5, 17.5, 12.5, 7.5, 2.5, −2.5. It never landed on zero.
And from −2.5 there's no way back. When you open the game, it goes back to taking off half a second at a time, so the timer goes −3, −3.5, −4, and keeps going down, forever. Leave the app open for a year and the next phase is due in −31 million seconds. Leave it open until the sun burns out and some phone in a drawer will say "next phase in −160,000,000,000,000,000 s" and mean it. The phase never changes again. Of the 60 values the timer can have, only 6 are multiples of five, so nine times out of ten, leaving the game for a while would freeze the Turning for good.
Here's the same check as the game, with both kinds of step. Each dot is a value the timer actually stopped on, and every time it hits zero and starts again from 30, it moves up a row.
The phase only changes when the timer is exactly 0.
The fix
The obvious fix is to change the check to "zero or less". The trouble is what happens next. If a step takes the timer from 2.5 to −2.5 and the game resets it to 30, those extra 2.5 seconds are lost. Over two hours of replay that adds up, and the phases end up somewhere different from where they'd be if you'd kept the app open. The whole point of the replay is that coming back should look like you never left.
So the fix is the boring one. Once you've unlocked the Turning, the replay goes back to half-second steps:
return state.unlocks >= 35 // the Turning exists
? 500 // half-second steps, ms
: 5_000;
That's ten times as many steps, 14,400 instead of 1,440, which is a lot of work to do while you're waiting to see your gold. But it only applies to players who've got that far, and every one of those steps lands where it would have if the app had been open. The timer hits zero again.
Nobody ever saw it
I caught it while building the replay, before it shipped. So as far as I know, the Turning has never actually frozen on anyone's phone. This page is the only place it still does, on purpose, as many times as you like.