Skip to main content

cuqueclicker_lib/
app.rs

1//! Native runner. Two threads:
2//!
3//! - **main thread**: owns the terminal, renders snapshots of the game state,
4//!   captures crossterm events, normalizes them into [`crate::input::InputEvent`]s,
5//!   and feeds them to the platform-agnostic input router (which produces
6//!   [`Action`]s + mutates [`UiState`]). Never blocks the sim — a slow render
7//!   (SSH lag, terminal resize, stuck flush) is invisible to game logic.
8//! - **sim thread**: owns the canonical [`GameState`], runs the 20Hz tick
9//!   loop via [`crate::sim::sim_tick`], drains [`Action`]s, saves to disk
10//!   through the [`Persistence`] impl, and publishes snapshots via
11//!   [`ArcSwap`]. Tick cadence is driven by `mpsc::recv_timeout(until_next_tick)`,
12//!   so it wakes exactly on tick deadlines or incoming actions — no busy spin,
13//!   no lost ticks under arbitrary render delay.
14//!
15//! The cross-platform half (input router, `apply_action`, `sim_tick`) is in
16//! `src/input.rs` and `src/sim.rs`. This file owns native-specific glue:
17//! crossterm event translation, threading, save scheduling, and the
18//! demo-recorder autopilot.
19
20use anyhow::Result;
21use arc_swap::ArcSwap;
22use crossterm::event::{
23    self, Event, KeyCode as CtKeyCode, KeyEventKind, KeyModifiers, MouseButton as CtMouseButton,
24    MouseEvent as CtMouseEvent, MouseEventKind,
25};
26use rand::RngExt;
27use ratatui::{Terminal, prelude::*};
28use std::sync::{
29    Arc,
30    atomic::{AtomicBool, Ordering},
31    mpsc,
32};
33use std::thread;
34use std::time::{Duration, Instant};
35
36use crate::game::achievement::ACHIEVEMENTS;
37use crate::game::fingerer;
38use crate::game::fingerer::FINGERERS;
39use crate::game::golden::{self, GoldenVariant};
40use crate::game::state::{GameState, TICK_HZ};
41use crate::game::upgrade::UPGRADES;
42use crate::input::{
43    self, InputContext, InputEvent, KeyCode as InKeyCode, Modifiers, MouseButton as InMouseButton,
44    UiState, WheelDelta,
45};
46use crate::platform::Persistence;
47use crate::sim::{self, Action, SimGeometry};
48use crate::ui::{self, Mode};
49
50const SAVE_INTERVAL_TICKS: u64 = TICK_HZ as u64 * 10;
51// Golden cooldown override used only during demo recording so the viewer
52// sees buffs/flashes frequently within the short clip.
53const DEMO_GOLDEN_COOLDOWN: u32 = 40;
54// Input poll timeout on the main thread — sets render responsiveness
55// when there's no input. At 16ms we redraw ~60Hz; snapshots advance
56// at 20Hz, so visual updates land within one frame of their tick.
57const INPUT_POLL_MS: u64 = 16;
58// How far behind we let the sim fall before we give up on catching up
59// (post-sleep, suspended SSH, etc.) and resync to wall clock. 20 ticks
60// = 1s at 20Hz.
61const MAX_TICK_CATCHUP: u32 = 20;
62
63/// Messages the sim thread sends back to main. Used exclusively by the
64/// demo driver to steer the on-camera panel cycle and to request a clean
65/// shutdown when the recording duration elapses.
66enum SimMsg {
67    DemoSetMode(Mode),
68    DemoQuit,
69}
70
71pub struct App {
72    state: GameState,
73    debug: bool,
74    demo_seconds: Option<u32>,
75    persistence: Persistence,
76}
77
78impl App {
79    pub fn new(
80        state: GameState,
81        debug: bool,
82        demo_seconds: Option<u32>,
83        persistence: Persistence,
84    ) -> Self {
85        Self {
86            state,
87            debug,
88            demo_seconds,
89            persistence,
90        }
91    }
92
93    pub fn run<B: Backend>(self, terminal: &mut Terminal<B>) -> Result<()>
94    where
95        B::Error: Send + Sync + 'static,
96    {
97        let App {
98            state,
99            debug,
100            demo_seconds,
101            persistence,
102        } = self;
103
104        let snapshot = Arc::new(ArcSwap::from_pointee(state.clone()));
105        let shutdown = Arc::new(AtomicBool::new(false));
106        let (action_tx, action_rx) = mpsc::channel::<Action>();
107        let (sim_msg_tx, sim_msg_rx) = mpsc::channel::<SimMsg>();
108
109        let sim_handle = {
110            let snapshot = snapshot.clone();
111            let shutdown = shutdown.clone();
112            thread::Builder::new()
113                .name("cuque-sim".into())
114                .spawn(move || {
115                    sim_loop(
116                        state,
117                        snapshot,
118                        action_rx,
119                        sim_msg_tx,
120                        shutdown,
121                        demo_seconds,
122                        persistence,
123                    );
124                })
125                .expect("spawn sim thread")
126        };
127
128        let mut ui = UiState::new();
129        let mut upgrade_rows: Vec<(usize, Rect)> = Vec::new();
130        let mut fingerer_rows: Vec<(usize, Rect)> = Vec::new();
131        let mut biscuit_rect = Rect::default();
132        let mut golden_rect = Rect::default();
133        let mut play_area = Rect::default();
134        // M1+M2: help-bar hint click rects + prestige-reset confirm rect.
135        // Both are recomputed every frame and consumed by the click router
136        // so the mouse-first player has equivalents for `[u]`, `[p]`,
137        // `[s]`, `[a]`, `[g]`, `[q]`, and the `[r] reset` confirm.
138        let mut help_hits: Vec<(crate::ui::HelpAction, Rect)> = Vec::new();
139        let mut prestige_reset_rect = Rect::default();
140        // Reused per-event scratch buffer — `process_input_event` appends
141        // produced actions here, then we drain it into the mpsc channel.
142        let mut actions: Vec<Action> = Vec::with_capacity(4);
143
144        while ui.running && !shutdown.load(Ordering::Relaxed) {
145            // Drain any panel/quit requests from the demo driver before we draw,
146            // so the frame we render reflects them.
147            for msg in sim_msg_rx.try_iter() {
148                match msg {
149                    SimMsg::DemoSetMode(m) => ui.mode = m,
150                    SimMsg::DemoQuit => ui.running = false,
151                }
152            }
153
154            let current = snapshot.load_full();
155            terminal.draw(|f| {
156                let out = ui::draw(f, &current, ui.mode, ui.zoom_idx, debug, ui.last_mouse_pos);
157                biscuit_rect = out.biscuit_rect;
158                golden_rect = out.golden_rect;
159                play_area = out.play_area;
160                upgrade_rows = out.upgrade_rows;
161                fingerer_rows = out.fingerer_rows;
162                help_hits = out.help_hits;
163                prestige_reset_rect = out.prestige_reset_rect;
164            })?;
165
166            // Hand fresh geometry to the sim. Ordering is preserved by mpsc,
167            // so the sim always uses the most recently drawn layout.
168            let _ = action_tx.send(Action::UpdateGeometry {
169                biscuit: biscuit_rect,
170            });
171
172            if event::poll(Duration::from_millis(INPUT_POLL_MS))? {
173                let ctx = InputContext {
174                    fingerer_rows: &fingerer_rows,
175                    upgrade_rows: &upgrade_rows,
176                    help_hits: &help_hits,
177                    biscuit_rect,
178                    golden_rect,
179                    play_area,
180                    prestige_reset_rect,
181                    debug,
182                    current: &current,
183                };
184                loop {
185                    let ev = event::read()?;
186                    if let Some(input_ev) = translate_crossterm(ev) {
187                        actions.clear();
188                        input::process_input_event(input_ev, &mut ui, &ctx, &mut actions);
189                        for a in actions.drain(..) {
190                            let _ = action_tx.send(a);
191                        }
192                    }
193                    if !event::poll(Duration::ZERO)? {
194                        break;
195                    }
196                }
197            }
198        }
199
200        // Tell sim to wind down, wait for it to flush state to disk.
201        shutdown.store(true, Ordering::Relaxed);
202        drop(action_tx);
203        sim_handle.join().expect("sim thread panicked");
204        Ok(())
205    }
206}
207
208// --- Sim thread ---------------------------------------------------------
209
210fn sim_loop(
211    mut state: GameState,
212    snapshot: Arc<ArcSwap<GameState>>,
213    actions: mpsc::Receiver<Action>,
214    sim_msg_tx: mpsc::Sender<SimMsg>,
215    shutdown: Arc<AtomicBool>,
216    demo_seconds: Option<u32>,
217    persistence: Persistence,
218) {
219    let tick_dt = Duration::from_micros(1_000_000 / TICK_HZ as u64);
220    let mut next_tick = Instant::now() + tick_dt;
221    let mut ticks_since_save: u64 = 0;
222    let mut demo_ticks: u64 = 0;
223    let mut demo_golden_spawns: u32 = 0;
224    let mut geom = SimGeometry::default();
225
226    loop {
227        if shutdown.load(Ordering::Relaxed) {
228            break;
229        }
230
231        // Block until the next tick deadline OR an action arrives — whichever
232        // comes first. No busy spin, no tick drift.
233        let timeout = next_tick.saturating_duration_since(Instant::now());
234        match actions.recv_timeout(timeout) {
235            Ok(action) => sim::apply_action(&mut state, action, &mut geom),
236            Err(mpsc::RecvTimeoutError::Timeout) => {}
237            Err(mpsc::RecvTimeoutError::Disconnected) => break,
238        }
239
240        // Run every tick we're behind on. If we've fallen absurdly far
241        // behind (laptop sleep), snap forward rather than grind through
242        // thousands of catch-up ticks.
243        let mut catchup = 0u32;
244        while Instant::now() >= next_tick {
245            sim::sim_tick(&mut state, &geom);
246            // Native-only post-tick concerns: demo-recorder autopilot and
247            // periodic save scheduling. Both are wrapped around the
248            // platform-agnostic `sim::sim_tick` call above.
249            if demo_seconds.is_some() {
250                demo_driver_tick(
251                    &mut state,
252                    &geom,
253                    demo_seconds,
254                    &mut demo_ticks,
255                    &mut demo_golden_spawns,
256                    &sim_msg_tx,
257                );
258            } else {
259                ticks_since_save += 1;
260                if ticks_since_save >= SAVE_INTERVAL_TICKS {
261                    ticks_since_save = 0;
262                    let _ = persistence.save(&state);
263                }
264            }
265            next_tick += tick_dt;
266            catchup += 1;
267            if catchup >= MAX_TICK_CATCHUP && Instant::now() > next_tick {
268                next_tick = Instant::now() + tick_dt;
269                break;
270            }
271        }
272
273        // Publish the new snapshot. Cheap clone (few small HashMaps + a
274        // short Vec of particles); Arc swap is lock-free.
275        snapshot.store(Arc::new(state.clone()));
276    }
277
278    // Graceful shutdown: one last achievement sweep and a final save. Demo
279    // mode runs on ephemeral state and never touches disk.
280    if demo_seconds.is_none() {
281        state.tick_achievements();
282        let _ = persistence.save(&state);
283    }
284}
285
286/// Demo-mode autopilot, running on the sim thread. Mutates state directly
287/// for clicks/buys; sends `SimMsg` back to main for panel swaps and the
288/// final quit signal since `mode` lives on the render thread.
289fn demo_driver_tick(
290    state: &mut GameState,
291    geom: &SimGeometry,
292    demo_seconds: Option<u32>,
293    demo_ticks: &mut u64,
294    demo_golden_spawns: &mut u32,
295    sim_msg_tx: &mpsc::Sender<SimMsg>,
296) {
297    *demo_ticks += 1;
298    let t = *demo_ticks;
299    let mut rng = rand::rng();
300
301    // ~1.5 clicks/s. Real play is faster; on camera it'd smear.
302    if t.is_multiple_of(13) {
303        let r = geom.biscuit;
304        if r.width > 0 && r.height > 0 {
305            state.click((r.x + r.width / 2, r.y + r.height / 2), r);
306        }
307    }
308
309    // Keep the screen busy with goldens: tighter cooldown than normal.
310    if state.golden.is_none() && state.golden_cooldown == 0 {
311        state.golden_cooldown = DEMO_GOLDEN_COOLDOWN;
312    }
313
314    // Force the variant on freshly-spawned goldens so the clip deterministically
315    // cycles Buff → Frenzy → Lucky. Buff comes first so a viewer definitely
316    // sees the purple powerup.
317    if let Some(g) = &mut state.golden
318        && g.life_ticks == golden::GOLDEN_LIFE_TICKS
319    {
320        g.variant = match *demo_golden_spawns % 3 {
321            0 => GoldenVariant::Buff,
322            1 => GoldenVariant::Frenzy,
323            _ => GoldenVariant::Lucky,
324        };
325        *demo_golden_spawns += 1;
326    }
327
328    // Auto-catch whatever golden is on screen after a brief "reaction
329    // time" so the marker is actually visible before disappearing.
330    if let Some(g) = &state.golden
331        && g.life_ticks + 20 < golden::GOLDEN_LIFE_TICKS
332    {
333        state.catch_golden();
334    }
335
336    // Every ~4s, buy 1-2 of a random affordable fingerer.
337    if t.is_multiple_of(80) {
338        let candidates: Vec<usize> = (0..fingerer::count())
339            .filter(|&i| state.can_buy(i))
340            .collect();
341        if !candidates.is_empty() {
342            let idx = candidates[rng.random_range(0..candidates.len())];
343            state.buy_n(idx, rng.random_range(1..=2));
344        }
345    }
346
347    // Every ~8s, buy the cheapest available upgrade.
348    if t.is_multiple_of(160) {
349        let available = crate::game::upgrade::available_ids(state);
350        if let Some(&u_idx) = available
351            .iter()
352            .min_by(|&&a, &&b| UPGRADES[a].cost.partial_cmp(&UPGRADES[b].cost).unwrap())
353        {
354            state.buy_upgrade(u_idx);
355        }
356    }
357
358    // Every ~15s, show a non-game panel for ~2s.
359    let phase = t % 300;
360    let panel_swap = if phase == 100 {
361        Some(Mode::Stats)
362    } else if phase == 140 {
363        Some(Mode::Achievements)
364    } else if phase == 180 {
365        Some(Mode::Upgrades)
366    } else if phase == 220 {
367        Some(Mode::Game)
368    } else {
369        None
370    };
371    if let Some(m) = panel_swap {
372        let _ = sim_msg_tx.send(SimMsg::DemoSetMode(m));
373    }
374
375    // Deadline: auto-quit when the user's requested duration elapses so the
376    // asciinema recording sees a clean exit.
377    if let Some(secs) = demo_seconds
378        && t >= (secs as u64) * (TICK_HZ as u64)
379    {
380        let _ = sim_msg_tx.send(SimMsg::DemoQuit);
381    }
382}
383
384// --- crossterm → InputEvent translation --------------------------------
385
386/// Normalize one crossterm event into our platform-neutral [`InputEvent`].
387/// Returns `None` for events we drop entirely (focus, paste, resize, key
388/// release/repeat, and unsupported mouse kinds).
389fn translate_crossterm(ev: Event) -> Option<InputEvent> {
390    match ev {
391        Event::Key(k) if k.kind == KeyEventKind::Press => {
392            let code = translate_key_code(k.code)?;
393            Some(InputEvent::KeyPress {
394                code,
395                mods: translate_mods(k.modifiers),
396            })
397        }
398        Event::Mouse(m) => translate_mouse(m),
399        _ => None,
400    }
401}
402
403fn translate_key_code(code: CtKeyCode) -> Option<InKeyCode> {
404    match code {
405        CtKeyCode::Char(c) => Some(InKeyCode::Char(c)),
406        CtKeyCode::Esc => Some(InKeyCode::Esc),
407        CtKeyCode::F(n) => Some(InKeyCode::F(n)),
408        _ => None,
409    }
410}
411
412fn translate_mods(mods: KeyModifiers) -> Modifiers {
413    Modifiers {
414        shift: mods.contains(KeyModifiers::SHIFT),
415        alt: mods.contains(KeyModifiers::ALT),
416        ctrl: mods.contains(KeyModifiers::CONTROL),
417    }
418}
419
420/// Narrow crossterm's mouse button to the subset the game cares about.
421/// Middle-click is intentionally dropped at the adapter boundary so it
422/// stays a no-op (matching pre-refactor behavior, where `handle_event`
423/// only matched `Down(Left)` / `Down(Right)`).
424fn translate_mouse_button(button: CtMouseButton) -> Option<InMouseButton> {
425    match button {
426        CtMouseButton::Left => Some(InMouseButton::Left),
427        CtMouseButton::Right => Some(InMouseButton::Right),
428        CtMouseButton::Middle => None,
429    }
430}
431
432fn translate_mouse(m: CtMouseEvent) -> Option<InputEvent> {
433    let mods = translate_mods(m.modifiers);
434    match m.kind {
435        MouseEventKind::Down(button) => Some(InputEvent::MouseDown {
436            col: m.column,
437            row: m.row,
438            button: translate_mouse_button(button)?,
439            mods,
440        }),
441        MouseEventKind::ScrollUp => Some(InputEvent::Wheel {
442            col: m.column,
443            row: m.row,
444            delta: WheelDelta::Up,
445        }),
446        MouseEventKind::ScrollDown => Some(InputEvent::Wheel {
447            col: m.column,
448            row: m.row,
449            delta: WheelDelta::Down,
450        }),
451        // K5: track mouse position for hover highlighting. Crossterm only
452        // emits Moved/Drag events when AnyMotion mouse mode is enabled
453        // (it is). Drag-with-left collapses to a plain Moved on the
454        // platform-neutral side; the renderer doesn't care.
455        MouseEventKind::Moved | MouseEventKind::Drag(CtMouseButton::Left) => {
456            Some(InputEvent::MouseMoved {
457                col: m.column,
458                row: m.row,
459            })
460        }
461        _ => None,
462    }
463}
464
465// --- Demo state --------------------------------------------------------
466
467/// Rich starting state for `--demo-for-recording`. Tuned so a viewer
468/// sees **numbers moving fast** (high FPS → counter spins visibly) and
469/// **many rings of hands** around the biscuit (heavy owned counts).
470/// Starting cuques is intentionally modest relative to FPS so the HUD
471/// counter grows by a visible fraction every frame instead of looking
472/// frozen.
473pub fn build_demo_state() -> GameState {
474    let mut s = GameState {
475        // Low relative to FPS so the counter clearly grows throughout
476        // the clip, and cheap enough to buy early tiers often.
477        cuques: 500_000.0,
478        lifetime_cuques: 500_000_000.0, // unlocks all tiers via the visibility gate
479        total_clicks: 500,
480        total_play_ticks: 3600 * TICK_HZ as u64, // pretend we've been at this an hour
481        prestige: 3,
482        golden_caught: 7,
483        // Default is a random 20-80s wait; force 0 so the first demo golden
484        // (a Buff, per the cycle in demo_driver_tick) spawns on tick 1 —
485        // the purple powerup lands well within the first few seconds of the clip.
486        golden_cooldown: 0,
487        best_fps: 50_000.0,
488        ..GameState::default()
489    };
490    // Seed counts/flags BY CATALOG INDEX rather than by hardcoded id strings,
491    // so a future rename/reorder/removal of a fingerer or upgrade can never
492    // silently degrade the demo (the live id at that slot is always used).
493    //
494    // Per-tier owned counts ramp down 40→10 across the first 8 fingerers.
495    // The per-type cap in `ui/hands.rs` is 40, so anything beyond that is
496    // visually identical — 8 types owned = thick crust of hands.
497    const DEMO_FINGERER_COUNTS: &[u32] = &[40, 40, 35, 30, 25, 20, 15, 10];
498    for (idx, &count) in DEMO_FINGERER_COUNTS.iter().enumerate() {
499        if let Some(f) = FINGERERS.get(idx)
500            && count > 0
501        {
502            s.fingerers_owned.insert(f.id.to_string(), count);
503        }
504    }
505    // Take the first 10 upgrades from the catalog (deterministic regardless
506    // of how UPGRADES is reordered) — gives a spread of click + per-tier
507    // multipliers so the sidebar shows (xN) on several tiers.
508    for u in UPGRADES.iter().take(10) {
509        s.upgrades_earned.insert(u.id.to_string());
510    }
511    // First 6 achievements for visual variety in that panel.
512    for a in ACHIEVEMENTS.iter().take(6) {
513        s.achievements_earned.insert(a.id.to_string());
514    }
515    s
516}