pmotschmann / Evolve

An incremental game about evolving a civilization
Mozilla Public License 2.0
824 stars 354 forks source link

Adding accelerated time for time the system was suspended/hibernated, two bugfixes of accelerated time #1030

Closed xilexio closed 6 months ago

xilexio commented 8 months ago

Added calcATime() in the longLoop so that accelerated time is added wheneever the loop took longer than the threshold from calcATime (currently 120s), indicating that there was system suspension, hibernation or something similar that made the game essentially pause. Before, the accelerated time was not given for that time. This fixes #972. It was tested with system hibernation.

xilexio commented 7 months ago

It seems that in practice the accelerated time using calcATime() is added even when not supposed to, so I need to investigate this further and check whether it is Chrome that runs JS less frequently than 120s or if there is another issue with adding accelerated time. Seeing that there are problems on Chrome, I need to do some longer testing also on Firefox. I'll reopen the PR once I fix it.

xilexio commented 7 months ago

I'm reopening the PR. I fixed the issues I noticed. I also fixed another bug. Accelerated time works by computing the number of days the accelerated time was supposed to happen. According to wiki, we expect to get 2/3 of the paused time as accelerated time (with some rounding and when it's at least 120s of wait). Therefore, the following equality should be satisfied: number of accelerated days * duration of one day / acceleration factor = time spent paused * 2 / 3 This was not the case. It was time spent paused / 3333ms, while it should be time spent paused / 2500ms * 2 / 3 = time spent paused / 3750ms.

The other thing that was wrong with it is not taking into account hyper and slow traits - the duration of the day was fixed like for normal races, but a day (interval of longLoop) is shorter for the hyper race. Effectively, hyper/slow races got the same number of accelerated days as normal races would, which would take a different amount of time than 2/3 of paused time.

I also renamed calcATime to addATime to make it clear that it modifies the state. I refactored computing length of loops to another function to expose this functionality to the function accelerating time. I also moved the acceleration factor (2) into a constant, so this may be fiddled with without breaking accelerated time.

I will keep testing this PR for a prolonged time, but so far values of accelerated time gained after pause or system hibernation or combination thereof seem to match expected values.

xilexio commented 6 months ago

I tested it over a few weeks, including with hyper and slow races, and it seems to work. I tested by noting down time of pause or hibernation/sleep and unpause/wake up and manually checking if the accelerated time is 2/3 of paused time, multiple times.