Reference architecture for browser idle games: state, generators, prestige, offline sim, save. Vanilla JS + break_infinity.js + EventBus.
  • JavaScript 76.1%
  • HTML 15.3%
  • Python 6.5%
  • CSS 2.1%
Find a file
2026-09-21 21:51:24 -04:00
.planning phase-10 commit D: scientist multi-currency (accepts[] shape + picker) 2026-08-27 17:00:14 -04:00
_art-practice/stairway-2026-08-27 phase-10 E-stairs: rename to base-stairway + 6 per-floor colour variants 2026-08-27 13:01:03 -04:00
assets Add ore sprite renders (10 biomes + 4 base resources + 3 fossil kinds) 2026-09-21 21:51:24 -04:00
data/fragments v4: Phase 9 commit 1 — fragment-kinds-pool + fragmentTuning + carryover tickResearch fix 2026-08-26 13:00:16 -04:00
docs phase-10 E-stairs: rename to base-stairway + 6 per-floor colour variants 2026-08-27 13:01:03 -04:00
scripts phase-10 commit U: fix layout + default drawer expanded 2026-08-28 12:30:33 -04:00
src v0.4-phase9: fix overlay reopen + save persistence (OG 2026-08-28) 2026-08-28 14:59:43 -04:00
test phase-10 commit N: live stairway placement editor (debug-only) 2026-08-28 00:36:11 -04:00
vendor phase-10 prep #3: glTF publishing layer + purity gate + self-hosted Draco/KTX2 2026-08-26 20:10:19 -04:00
.gitignore phase-10 preflight: ground-removal + thumbnail regeneration + plan 2026-08-26 23:28:48 -04:00
HANDOFF.md DMR handoff notes (project pause 2026-08-28) 2026-08-28 15:29:22 -04:00
index.html v0.4-phase9: fix overlay reopen + save persistence (OG 2026-08-28) 2026-08-28 14:59:43 -04:00
main.mjs v15: copy hydration-aware Renderer.js 2026-07-18 11:19:47 -04:00
package-lock.json v4: Phase 2 commits 5-6 — save-restore test + SUMMARY + STATE close 2026-08-25 11:40:19 -04:00
package.json phase-10 E-stairs: rename to base-stairway + 6 per-floor colour variants 2026-08-27 13:01:03 -04:00
README.md v6: add AutoPrestigeManager 2026-07-18 08:45:22 -04:00
RESEARCH_SYNTHESIS.md v4: Phase 4 commit 1 — three-scene-init (scene+camera+lights) 2026-08-25 19:21:43 -04:00
SPEC.md npc-roster: canonical Geologist/Archaeologist/Survivalist/Oracle (L3-L6) 2026-08-27 13:20:33 -04:00
VISUAL_BRIEF.md v4: Phase 1 design pass v0.3 — hybrid frame + multilevel hub + in-cart POV 2026-08-25 11:28:18 -04:00

Idle Scaffold — Reference Architecture

A working browser idle game skeleton with the fifteen systems every idle game needs. v6 adds the AutoPrestigeManager (per-layer "double-or-nothing" automation, with cascade support across multi-layer prestige).

Module What it does
utils/Decimal.js Big-number math (break_infinity.js) + number formatter
core/EventBus.js Pub/sub so systems don't couple to each other
core/GameState.js Single source of truth, with serialization
core/GameLoop.js rAF-driven master tick
generators/Generator.js Cost formula, production formula, max-affordable math
prestige/PrestigeLayer.js Reset logic + currency earn preview
achievements/AchievementSystem.js Mechanically-meaningful achievements (CC milk-upgrade pattern)
synergies/SynergyRegistry.js Sparse, asymmetric, conditional cross-system bonuses
upgrades/UpgradeSystem.js Three-shape upgrades (multiply / percentage / additive) with stack caps
buffs/BuffSystem.js Temporary multiplicative buffs (Frenzy, Click Frenzy, etc.)
events/GoldenCookie.js Random spawner with rarity-weighted buff kinds
iap/TimeSkipManager.js Premium skip + ad-rewarded full-offline claim
automation/AutoBuyManager.js Per-generator automated purchasing (1Hz decisions)
automation/AutoPrestigeManager.js Per-layer "double-or-nothing" auto-prestige, with cascade
offline/OfflineSimulator.js simulateSimple (constant-rate) + simulateAccurate (compressed-tick)
saves/SaveManager.js localStorage autosave with dirty-count + debounce
ui/Renderer.js Framework-free DOM rendering
main.js Wires it all together with sample content

Run it

cd idle-scaffold
python3 -m http.server 5173
# open http://localhost:5173

You'll see a 4-generator economy (Lemonade Stand, Newspaper, Pizza, Shrimp), two prestige layers ("Hire Angels" → "Enter Eternity"), 6 achievements, and 3 synergies. Open DevTools console — window.__idle exposes the game state for poking.

Tests

node test/smoke.mjs

132/132 passing, covering:

  • Generator cost scaling (exponential)
  • Max-affordable geometric-series formula
  • Production with multiplicative multipliers
  • Prestige sqrt scaling (Angels)
  • Big-number precision past JS Number limit
  • Achievement system — event-driven unlock, idempotency, cumulative bonus math
  • Synergy registry — sparse, asymmetric, conditional multipliers
  • Two-layer prestige — Angels gate, reset, Eternity gate, reset, re-gating
  • Upgrade system — three shapes, maxStack enforcement, cost scaling, insufficient-funds check, fullBonus aggregator shape
  • Buff system — Frenzy/Click-Frenzy/Lucky multiplicative stacking, expiry via tick, prestige clears
  • Golden Cookie — random spawn with rarity weights, 13s lifetime, click-to-collect, expire-on-tick, prestige clears
  • Time-skip IAP — gem cost calculation, deducts premium currency, offline-cap enforcement, ad-rewarded 2x full-offline
  • AutoBuyManager — toggle, plan(), check-interval throttle, bulk geometric-sum purchase, stats tracking, disableAll
  • simulateAccurate — compressed-tick simulation, auto-buy integration, cap enforcement, transition counting
  • AutoPrestigeManager — toggle, double-or-nothing decision, cooldown, cascade across layers, disableAll

Architecture decisions

  1. No framework. Vanilla JS + a small render loop. Idle UIs update at most every 50-100ms — a 5kb renderer is enough. Migrate to Svelte/React when you have 50+ components, not before.

  2. Event bus over direct calls. Every cross-system update goes through EventBus.emit(). Achievement system, UI, saves, and analytics stay decoupled. This is the difference between an idle codebase that scales to 10 prestige layers and one that doesn't.

  3. Decimal everywhere. break_infinity.js (Patashu) — fast, scales to 1e9e15. For 10^^N numbers (Antimatter Dimensions late game) swap to break_eternity.js. Don't use raw JS Number — you lose precision at 1e16 and overflow at 1.79e308.

  4. rAF-driven tick, clamped to 1s. Tabs in the background throttle rAF to ~1Hz. We clamp dt to 1000ms so a 30-minute background tab doesn't credit 30 minutes of production in one frame.

  5. Save debouncing on events, not on wall-clock. The save manager listens to GENERATOR_BOUGHT / UPGRADE_BOUGHT / PRESTIGE events and marks dirty. Autosave fires every 10s OR after 5 mutations. This gives burst protection (rapid clicking won't lose data) without thrashing localStorage.

  6. Offline simulation is decoupled. OfflineSimulator takes the current state and a time delta, returns a report. Swap in the simulateAccurate() implementation later without touching the game loop or state code.

  7. Achievements are mechanically meaningful. Cookie Clicker's milk upgrades tie to achievement % — we follow the same pattern. Each achievement contributes a small multiplicative bonus (1-10%) so the cumulative effect of 50+ achievements is the "achievement bonus." Cosmetic achievements are wasted design space in a no-failure-state game.

  8. Synergies are sparse and asymmetric. 5-8 meaningful ones, not 20+. Each is a function from state → multiplier. Conditional ones ("only while you have >50 newspaper") force active management and create the "feed" pattern of A-boosts-B-boosts-C.

Multiplier stack

All bonuses combine multiplicatively into a single multipliers Decimal that gets multiplied into production. The formula:

production = ( base * owned + upgAdd ) *
    (1 + 0.02 * angels)              ← Layer 0 passive
  * (1 + 0.05 * eternity_points)     ← Layer 1 passive
  * achievementBonus()               ← All unlocked achievements
  * synergyBonus(state)             ← All active synergies
  * upgMultiply                      ← All stackable multiply upgrades
  * buffBonus                        ← Active Golden Cookie buffs
  * (1 + (upgPct + otherPct) / 100)  ← Sum-of-percentages bucket

This is the single most important property of the architecture: one math formula, one source of truth, every system contributes by multiplying.

Pitfalls the scaffold handles for you

  • ✅ Decimal precision (1e9e15+)
  • ✅ Save format versioning
  • ✅ Save errors don't crash the game loop
  • ✅ Tab-backgrounding doesn't trigger massive catch-up
  • ✅ EventBus exceptions don't kill the loop
  • ✅ Number formatting across all size regimes
  • ✅ Achievement idempotency (re-check doesn't double-fire)
  • ✅ Multi-layer prestige with re-gating after reset
  • ✅ AutoBuy / AutoPrestige with check-interval throttling and cascade

What to add next

  • Cloud saves — drop in a CloudSaveManager that mirrors SaveManager.
  • IAP / real-money monetization — requires app-store SDKs, not in scaffold.
  • Random-event offline — weighted random outcomes during offline sim (Tideward's "found a rare ore!" pattern).
  • Multi-generator auto-buy strategies — highest-mult-per-cost, by-rate-of- return, etc. Scaffold ships with cheapest-affordable; pluggable via the strategy constructor arg.
  • Auto-prestige integration with offline sim — simulateAccurate should fire auto-prestige during the simulated window when both are enabled.