1use 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;
51const DEMO_GOLDEN_COOLDOWN: u32 = 40;
54const INPUT_POLL_MS: u64 = 16;
58const MAX_TICK_CATCHUP: u32 = 20;
62
63enum 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 green_coin_rect = Rect::default();
134 let mut play_area = Rect::default();
135 let mut help_hits: Vec<(crate::ui::HelpAction, Rect)> = Vec::new();
140 let mut prestige_reset_rect = Rect::default();
141 let mut actions: Vec<Action> = Vec::with_capacity(4);
144
145 while ui.running && !shutdown.load(Ordering::Relaxed) {
146 for msg in sim_msg_rx.try_iter() {
149 match msg {
150 SimMsg::DemoSetMode(m) => ui.mode = m,
151 SimMsg::DemoQuit => ui.running = false,
152 }
153 }
154
155 let current = snapshot.load_full();
156 terminal.draw(|f| {
157 let out = ui::draw(f, ¤t, ui.mode, ui.zoom_idx, debug, ui.last_mouse_pos);
158 biscuit_rect = out.biscuit_rect;
159 golden_rect = out.golden_rect;
160 green_coin_rect = out.green_coin_rect;
161 play_area = out.play_area;
162 upgrade_rows = out.upgrade_rows;
163 fingerer_rows = out.fingerer_rows;
164 help_hits = out.help_hits;
165 prestige_reset_rect = out.prestige_reset_rect;
166 })?;
167
168 let _ = action_tx.send(Action::UpdateGeometry {
171 biscuit: biscuit_rect,
172 });
173
174 if event::poll(Duration::from_millis(INPUT_POLL_MS))? {
175 let ctx = InputContext {
176 fingerer_rows: &fingerer_rows,
177 upgrade_rows: &upgrade_rows,
178 help_hits: &help_hits,
179 biscuit_rect,
180 golden_rect,
181 green_coin_rect,
182 play_area,
183 prestige_reset_rect,
184 debug,
185 current: ¤t,
186 };
187 loop {
188 let ev = event::read()?;
189 if let Some(input_ev) = translate_crossterm(ev) {
190 actions.clear();
191 input::process_input_event(input_ev, &mut ui, &ctx, &mut actions);
192 for a in actions.drain(..) {
193 let _ = action_tx.send(a);
194 }
195 }
196 if !event::poll(Duration::ZERO)? {
197 break;
198 }
199 }
200 }
201 }
202
203 shutdown.store(true, Ordering::Relaxed);
205 drop(action_tx);
206 sim_handle.join().expect("sim thread panicked");
207 Ok(())
208 }
209}
210
211fn sim_loop(
214 mut state: GameState,
215 snapshot: Arc<ArcSwap<GameState>>,
216 actions: mpsc::Receiver<Action>,
217 sim_msg_tx: mpsc::Sender<SimMsg>,
218 shutdown: Arc<AtomicBool>,
219 demo_seconds: Option<u32>,
220 persistence: Persistence,
221) {
222 let tick_dt = Duration::from_micros(1_000_000 / TICK_HZ as u64);
223 let mut next_tick = Instant::now() + tick_dt;
224 let mut ticks_since_save: u64 = 0;
225 let mut demo_ticks: u64 = 0;
226 let mut demo_golden_spawns: u32 = 0;
227 let mut geom = SimGeometry::default();
228
229 loop {
230 if shutdown.load(Ordering::Relaxed) {
231 break;
232 }
233
234 let timeout = next_tick.saturating_duration_since(Instant::now());
237 match actions.recv_timeout(timeout) {
238 Ok(action) => sim::apply_action(&mut state, action, &mut geom),
239 Err(mpsc::RecvTimeoutError::Timeout) => {}
240 Err(mpsc::RecvTimeoutError::Disconnected) => break,
241 }
242
243 let mut catchup = 0u32;
247 while Instant::now() >= next_tick {
248 sim::sim_tick(&mut state, &geom);
249 if demo_seconds.is_some() {
253 demo_driver_tick(
254 &mut state,
255 &geom,
256 demo_seconds,
257 &mut demo_ticks,
258 &mut demo_golden_spawns,
259 &sim_msg_tx,
260 );
261 } else {
262 ticks_since_save += 1;
263 if ticks_since_save >= SAVE_INTERVAL_TICKS {
264 ticks_since_save = 0;
265 let _ = persistence.save(&state);
266 }
267 }
268 next_tick += tick_dt;
269 catchup += 1;
270 if catchup >= MAX_TICK_CATCHUP && Instant::now() > next_tick {
271 next_tick = Instant::now() + tick_dt;
272 break;
273 }
274 }
275
276 snapshot.store(Arc::new(state.clone()));
279 }
280
281 if demo_seconds.is_none() {
284 state.tick_achievements();
285 let _ = persistence.save(&state);
286 }
287}
288
289fn demo_driver_tick(
293 state: &mut GameState,
294 geom: &SimGeometry,
295 demo_seconds: Option<u32>,
296 demo_ticks: &mut u64,
297 demo_golden_spawns: &mut u32,
298 sim_msg_tx: &mpsc::Sender<SimMsg>,
299) {
300 *demo_ticks += 1;
301 let t = *demo_ticks;
302 let mut rng = rand::rng();
303
304 if t.is_multiple_of(13) {
306 let r = geom.biscuit;
307 if r.width > 0 && r.height > 0 {
308 state.click((r.x + r.width / 2, r.y + r.height / 2), r);
309 }
310 }
311
312 if state.golden.is_none() && state.golden_cooldown == 0 {
314 state.golden_cooldown = DEMO_GOLDEN_COOLDOWN;
315 }
316
317 if let Some(g) = &mut state.golden
321 && g.life_ticks == golden::GOLDEN_LIFE_TICKS
322 {
323 g.variant = match *demo_golden_spawns % 3 {
324 0 => GoldenVariant::Buff,
325 1 => GoldenVariant::Frenzy,
326 _ => GoldenVariant::Lucky,
327 };
328 *demo_golden_spawns += 1;
329 }
330
331 if let Some(g) = &state.golden
334 && g.life_ticks + 20 < golden::GOLDEN_LIFE_TICKS
335 {
336 state.catch_golden();
337 }
338
339 if t.is_multiple_of(80) {
341 let candidates: Vec<usize> = (0..fingerer::count())
342 .filter(|&i| state.can_buy(i))
343 .collect();
344 if !candidates.is_empty() {
345 let idx = candidates[rng.random_range(0..candidates.len())];
346 state.buy_n(idx, rng.random_range(1..=2));
347 }
348 }
349
350 if t.is_multiple_of(160) {
352 let available = crate::game::upgrade::available_ids(state);
353 if let Some(&u_idx) = available
354 .iter()
355 .min_by(|&&a, &&b| UPGRADES[a].cost.partial_cmp(&UPGRADES[b].cost).unwrap())
356 {
357 state.buy_upgrade(u_idx);
358 }
359 }
360
361 let phase = t % 300;
363 let panel_swap = if phase == 100 {
364 Some(Mode::Stats)
365 } else if phase == 140 {
366 Some(Mode::Achievements)
367 } else if phase == 180 {
368 Some(Mode::Upgrades)
369 } else if phase == 220 {
370 Some(Mode::Game)
371 } else {
372 None
373 };
374 if let Some(m) = panel_swap {
375 let _ = sim_msg_tx.send(SimMsg::DemoSetMode(m));
376 }
377
378 if let Some(secs) = demo_seconds
381 && t >= (secs as u64) * (TICK_HZ as u64)
382 {
383 let _ = sim_msg_tx.send(SimMsg::DemoQuit);
384 }
385}
386
387fn translate_crossterm(ev: Event) -> Option<InputEvent> {
393 match ev {
394 Event::Key(k) if k.kind == KeyEventKind::Press => {
395 let code = translate_key_code(k.code)?;
396 Some(InputEvent::KeyPress {
397 code,
398 mods: translate_mods(k.modifiers),
399 })
400 }
401 Event::Mouse(m) => translate_mouse(m),
402 _ => None,
403 }
404}
405
406fn translate_key_code(code: CtKeyCode) -> Option<InKeyCode> {
407 match code {
408 CtKeyCode::Char(c) => Some(InKeyCode::Char(c)),
409 CtKeyCode::Esc => Some(InKeyCode::Esc),
410 CtKeyCode::F(n) => Some(InKeyCode::F(n)),
411 _ => None,
412 }
413}
414
415fn translate_mods(mods: KeyModifiers) -> Modifiers {
416 Modifiers {
417 shift: mods.contains(KeyModifiers::SHIFT),
418 alt: mods.contains(KeyModifiers::ALT),
419 ctrl: mods.contains(KeyModifiers::CONTROL),
420 }
421}
422
423fn translate_mouse_button(button: CtMouseButton) -> Option<InMouseButton> {
428 match button {
429 CtMouseButton::Left => Some(InMouseButton::Left),
430 CtMouseButton::Right => Some(InMouseButton::Right),
431 CtMouseButton::Middle => None,
432 }
433}
434
435fn translate_mouse(m: CtMouseEvent) -> Option<InputEvent> {
436 let mods = translate_mods(m.modifiers);
437 match m.kind {
438 MouseEventKind::Down(button) => Some(InputEvent::MouseDown {
439 col: m.column,
440 row: m.row,
441 button: translate_mouse_button(button)?,
442 mods,
443 }),
444 MouseEventKind::ScrollUp => Some(InputEvent::Wheel {
445 col: m.column,
446 row: m.row,
447 delta: WheelDelta::Up,
448 }),
449 MouseEventKind::ScrollDown => Some(InputEvent::Wheel {
450 col: m.column,
451 row: m.row,
452 delta: WheelDelta::Down,
453 }),
454 MouseEventKind::Moved | MouseEventKind::Drag(CtMouseButton::Left) => {
459 Some(InputEvent::MouseMoved {
460 col: m.column,
461 row: m.row,
462 })
463 }
464 _ => None,
465 }
466}
467
468pub fn build_demo_state() -> GameState {
477 let mut s = GameState {
478 cuques: 500_000.0,
481 lifetime_cuques: 500_000_000.0, total_clicks: 500,
483 total_play_ticks: 3600 * TICK_HZ as u64, prestige: 3,
485 golden_caught: 7,
486 golden_cooldown: 0,
490 best_fps: 50_000.0,
491 ..GameState::default()
492 };
493 const DEMO_FINGERER_COUNTS: &[u32] = &[40, 40, 35, 30, 25, 20, 15, 10];
501 for (idx, &count) in DEMO_FINGERER_COUNTS.iter().enumerate() {
502 if let Some(f) = FINGERERS.get(idx)
503 && count > 0
504 {
505 s.fingerers_state.entry(f.id.to_string()).or_default().count = count;
506 }
507 }
508 for u in UPGRADES.iter().take(10) {
512 s.upgrades_earned.insert(u.id.to_string());
513 }
514 for a in ACHIEVEMENTS.iter().take(6) {
516 s.achievements_earned.insert(a.id.to_string());
517 }
518 s
519}