The Math of Time Travel: Solving Offline Combat in Yokai Idle

How do you simulate 24 hours of combat in 13 seconds? We dive into the technical hurdles of tick rates, time dilation bugs, and achieving parity between active and offline play.

In an idle RPG, the 'Offline Progress' screen is the most important interaction a player has. It needs to feel rewarding, but more importantly, it needs to be accurate. If the offline simulation is faster than the live game, it’s an exploit; if it’s slower, it feels like a punishment.

Our first attempt at offline combat was a disaster of 'Time Dilation.' We were simulating combat in 500ms chunks but only subtracting 100ms from the player's 'Time Bank.' The result? Players were banishing 150 Yokai in 30 seconds—essentially playing the game at 5x speed.

The Simulation Synchronization Hurdles

  • Tick Rate Parity: Aligning the simulation's 'Delta Time' with the actual subtraction from the time bank
  • The Search Penalty: Factoring in the 2–3 seconds it takes to find a new monster, which is often forgotten in 'instant' code loops
  • Food Consumption Logic: Ensuring 'Auto-Eat' thresholds match the live game to prevent accidental player deaths
  • CPU Bottlenecks: Moving expensive .find() and .filter() calls outside the combat loop to prevent 13-second browser freezes

To solve the speed issues, we moved from a 100ms tick to a 500ms tick. This reduced the CPU overhead by 80% while keeping the math precise enough to track every hit, dodge, and special attack. We also implemented a mandatory 'Respawn Timer' in the code to mirror the 'Searching' state seen in the UI.

Optimizing for 80,000+ Calculations

  • Pre-calculating Monster Stats: Grabbing all level-based modifiers once before the combat loop begins
  • Special Attack Mapping: Creating a static Map of attack data to avoid repeated array searches
  • Ghost Resumption: Implementing logic to save partial progress if a player returns mid-dungeon
  • Death Punishment: Calculating the 'Time Lost' penalty accurately when a player runs out of food

The final result is a simulation that can process 80,000 Yokai banishments in roughly 13 seconds of 'Compute Time.' By tracking the execution time with performance.now(), we can ensure the main thread stays healthy while giving players a perfectly accurate summary of their time away.

Our next goal is moving this entire simulation into a Web Worker. This will allow the game to 'think' in the background while the UI stays butter-smooth, ensuring that even a 24-hour catch-up doesn't cause a single frame of lag.