tetro-tui 3.5.2

A terminal-based but modern tetromino-stacking game that is very customizable and cross-platform.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
use std::{
    io::{self, Write},
    sync::mpsc,
    time::{Duration, Instant},
};

use crate::{
    core_game_engine::{Button, Game, GameEndCause, Input, Notification, Phase, UpdateGameError},
    terminal_buffers::TermCell,
};
use crossterm::{
    ExecutableCommand,
    cursor::MoveTo,
    event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
    style::Print,
    terminal::{self},
};

use crate::{
    Application, EncodedInputHistory, GameMetaData, GameRestorationData, GameSave, RawInputHistory,
    ScoreSummary, Statistics,
    fmt_helpers::{fmt_button_keybinds, increment_game_mode_derivative},
    game_renderers::{GameRenderer, MiscGameRenderers, calc_game_keybinds_legend},
    game_restoration::{InputHistoryEncoder, QuantizeInGameTime},
    tui_menus::{Menu, MenuUpdate},
};

impl<W: Write> Application<W> {
    pub fn run_menu_play_game(
        &mut self,
        game: &mut Game,
        raw_input_history: &mut RawInputHistory,
        game_meta_data: &mut GameMetaData,
        game_renderer: &mut MiscGameRenderers,
        selection_id_for_game_retry: Option<usize>,
    ) -> io::Result<MenuUpdate> {
        /* Our game loop recipe looks like this:
          * Enter 'update_and_render loop:
            - If game has ended, break loop.
            - Enter 'wait loop (budget based on 'latest refresh'):
              + Use player input to update game.
              + If budget ran out, break loop.
            - Set 'latest refresh' variable to ::now().
            - Do game.update().
              ** Note that in-game time at time of update can be determined with either
                 -- `duration elapsed IRL - duration paused`,
                 -- `in-game time before entering loop + in-game time elapsed since loop entered`.

            - Do game.render().
            - Continue 'update_and_render.
        */

        // Prepare everything to enter the game (react & render) loop.

        // Toggle on enhanced-keyboard-events.
        if self.temp_data.kitty_assumed {
            let f = Self::GAME_KEYBOARD_ENHANCEMENT_FLAGS;
            // FIXME: Explicitly ignore an error when pushing flags. This is so we can still try even if Crossterm doesn't like operating on Windows.
            let _r = self.term.execute(event::PushKeyboardEnhancementFlags(f));
        }

        // Prepare channel from which to receive terminal inputs.
        let (input_sender, input_receiver) = mpsc::channel();

        // Spawn input catcher thread.
        // FIXME: Some terminals may have weird/slow treatment of `Esc` key, which causes a delay between pressing the key and us actually pausing the game.
        // So perhaps we should generalize this and allow players to customize TUI (menu navigation etc.) keybinds as well?
        let is_stop_event = |event: Event| {
            let Event::Key(KeyEvent {
                code,
                modifiers,
                kind: KeyEventKind::Press | KeyEventKind::Repeat,
                ..
            }) = event
            else {
                return false;
            };
            matches!(code, KeyCode::Esc | KeyCode::Char('?'))
                || matches!(
                    (code, modifiers),
                    (
                        KeyCode::Char(
                            'd' | 'D' | 'c' | 'C' | 'r' | 'R' /* Reset is done by pushing entirely new game menu. */
                        ),
                        KeyModifiers::CONTROL
                    )
                )
        };
        let _thread_handle = input_catcher::spawn(input_sender, is_stop_event);

        let mut temp_statistics = Statistics::default();

        // FIXME: Might falsely lead to a teleport if the player pressed move within the time window at the beginning.
        // But we don't care much, as for 'usual' values this should not really happen.
        // Stores `(last_time_move_pressed, was_left_not_right)`.
        let mut temp_last_move = (Instant::now(), false);

        let keybinds_legend = calc_game_keybinds_legend(self.settings.keybinds());

        // FPS counter.
        let mut renders_per_second_counter = 0u32;
        let mut renders_per_second_counter_start_time = Instant::now();

        // Initial render.
        game_renderer.reset_viewport_state(
            (0, 0),
            terminal::size().unwrap_or_default(),
            TermCell {
                bg: self.settings.tui_coloring().bg_tui,
                ..TermCell::BLANK
            },
        );
        game_renderer.render(
            &mut self.term,
            game,
            game_meta_data,
            &self.settings,
            &self.temp_data,
            &keybinds_legend,
            None,
        )?;

        // How much time passes between each refresh.
        let frame_interval = Duration::from_secs_f64(self.settings.graphics().fps.get().recip());

        // Time of the game when we enter the game loop.
        let mut ingametime_when_game_loop_entered = game.state().time;

        // The 'real-life' time at which we enter the game loop.
        let mut time_game_loop_entered = Instant::now();

        // The number of the frame. This is used to calculate the time of the next frame.
        let mut time_next_frame = time_game_loop_entered;

        // Main Game Loop

        let menu_update = 'update_and_render: loop {
            // Start new iteration of [render->input->] loop.

            if let Phase::GameEnd { cause, is_win } = game.phase() {
                self.statistics.games_ended += 1;

                // Game ended, cannot actually continue playing;
                // Convert to scoreboard entry and return appropriate game-ended menu.
                let scores_entry = ScoreSummary {
                    game_meta_data: game_meta_data.clone(),
                    is_win: *is_win,
                    end_cause: cause.clone(),
                    time: game.state().time,
                    pieces: game.state().pieces_locked,
                    lineclears: game.state().lineclears,
                    fall_delay_reached: game.state().fall_delay,
                    lock_delay_reached: game.state().lock_delay,
                    points: game.state().points,
                };

                let encoded_input_history = EncodedInputHistory::encode(raw_input_history);
                let forfeit =
                    matches!(cause, GameEndCause::Forfeit { .. }).then_some(game.state().time);

                let game_restoration_data =
                    GameRestorationData::new(game, encoded_input_history, forfeit);

                self.scores_and_replays
                    .entries
                    .insert(0, (scores_entry.clone(), Some(game_restoration_data)));

                let game_scoring = Box::new(scores_entry);

                let menu = if *is_win {
                    Menu::GameComplete { game_scoring }
                } else {
                    Menu::GameOver { game_scoring }
                };

                break 'update_and_render MenuUpdate::Push(menu);
            }

            // Calculate the time of the next render (according to frame_interval heartbeat) we can catch.
            // This means we skip renders if we missed their window anyway.
            let now = Instant::now();
            loop {
                time_next_frame += frame_interval;
                if time_next_frame < now {
                    continue;
                }
                break;
            }

            'wait: loop {
                // Compute duration left until we should stop waiting.
                let refresh_time_budget_remaining =
                    time_next_frame.saturating_duration_since(Instant::now());

                let recv_result = input_receiver.recv_timeout(refresh_time_budget_remaining);
                /* FIXME: Unused code: Reconsider tradeoff:
                The problem with the following code is a fine tradeoff between `std::mpsc::recv_timeout(rcvr, dt)` and `crossterm::event::poll(dt)`.
                The exact tradeoff is very unclear, but we trust the Rust stdlib for its slightly better performance/reliability
                in some ad-hoc testing, despite the 'direct' approach not requiring an input catcher thread.

                // Wait for poll response.
                let event_available = event::poll(refresh_time_budget_remaining)?;
                // Finished waiting with no terminal signal available.
                if !event_available {
                    break 'wait;
                }
                let timestamp = Instant::now();
                let event = event::read()?;
                */

                // Read terminal signal or finish waiting.
                let (event, timestamp) = match recv_result {
                    Err(recv_timeout_error) => match recv_timeout_error {
                        // Frame idle/budget expired on its own: leave wait loop.
                        mpsc::RecvTimeoutError::Timeout => {
                            break 'wait;
                        }

                        // Input handler thread died... Pause game for now.
                        mpsc::RecvTimeoutError::Disconnected => {
                            // FIXME: This 'extremely' rare error is currently fixed by pausing the game
                            // which means no extra work for us and just one extra step for the user.
                            // But maybe properly try restarting the thread manually?...
                            break 'update_and_render MenuUpdate::Push(Menu::Pause);
                        }
                    },
                    Ok(x) => x,
                };

                if let Event::Key(KeyEvent {
                    code,
                    modifiers,
                    kind,
                    state: _,
                }) = event
                    && let Some(mut button) =
                        self.settings.keybinds().get((code, modifiers)).copied()
                {
                    // We first calculate the intended time at time of reaching here.
                    let update_target_time = ingametime_when_game_loop_entered
                        + timestamp.saturating_duration_since(time_game_loop_entered);

                    // Guarantee update cannot fail because it lies in the game's past:
                    // Worst case react to player input as quickly as possible now.
                    let update_target_time = game.state().time.max(update_target_time);

                    // Lastly we (artificially) compress the information of when an input happened (we essentially round it).
                    let update_target_time = update_target_time.quantize();

                    if self.temp_data.kitty_assumed {
                        // Enhanced keyboard events: determinedly send a single press or release.

                        let mut player_input = (match kind {
                            KeyEventKind::Press => Input::Activate,
                            // Kitty does not actually care about terminal/OS keyboard 'repeat' events.
                            KeyEventKind::Repeat => continue 'wait,
                            KeyEventKind::Release => Input::Deactivate,
                        })(button);

                        // FIXME: We only transform `Activate`s into teleports currently,
                        // but we forget the release events (which will just be move releases,
                        // i.e. teleport will remain active).
                        // In usual games this will not lead to issues but logically unclean (also, modding behavior).
                        // We expect primary users of double-tap finesse to be non-enhanced-terminal users anyway.
                        if let Some(tap_move_delay) = self.settings.gameplay().dtapfinesse {
                            match player_input {
                                Input::Activate(Button::MoveLeft) => {
                                    if temp_last_move.1
                                        && timestamp.duration_since(temp_last_move.0)
                                            <= tap_move_delay
                                    {
                                        player_input = Input::Activate(Button::TeleLeft);
                                    }
                                    temp_last_move = (timestamp, true);
                                }
                                Input::Activate(Button::MoveRight) => {
                                    if !temp_last_move.1
                                        && timestamp.duration_since(temp_last_move.0)
                                            <= tap_move_delay
                                    {
                                        player_input = Input::Activate(Button::TeleRight);
                                    }
                                    temp_last_move = (timestamp, false);
                                }
                                _ => {}
                            }
                        }

                        raw_input_history
                            .inputs
                            .push((update_target_time, player_input));

                        match game.update(update_target_time, Some(player_input)) {
                            Ok(msgs) => {
                                temp_statistics.accumulate_from_feed(&msgs);
                                game_renderer.update_feed(msgs, &self.settings)
                            }
                            Err(UpdateGameError::AlreadyEnded) => break 'wait,
                            Err(UpdateGameError::TargetTimeInPast) => unreachable!(),
                        }
                    } else {
                        // Else: Non-kitty input handling.

                        // Special handling here for terminals that STILL send "release" events despite us assuming it's not enhanced;
                        // So we don't accidentally interpret them as presses.
                        if !matches!(kind, KeyEventKind::Press | KeyEventKind::Repeat) {
                            continue 'wait;
                        }

                        if let Some(tap_move_delay) = self.settings.gameplay().dtapfinesse {
                            match button {
                                Button::MoveLeft => {
                                    if temp_last_move.1
                                        && timestamp.duration_since(temp_last_move.0)
                                            <= tap_move_delay
                                    {
                                        button = Button::TeleLeft;
                                    }
                                    temp_last_move = (timestamp, true);
                                }
                                Button::MoveRight => {
                                    if !temp_last_move.1
                                        && timestamp.duration_since(temp_last_move.0)
                                            <= tap_move_delay
                                    {
                                        button = Button::TeleRight;
                                    }
                                    temp_last_move = (timestamp, false);
                                }
                                _ => {}
                            }
                        }

                        // Non-enhanced terminal - since we don't have "release" events, we just assume a button press is an instantaneous sequence of press+release.
                        let input = Input::Activate(button);

                        raw_input_history.inputs.push((update_target_time, input));

                        match game.update(update_target_time, Some(input)) {
                            Ok(msgs) => {
                                temp_statistics.accumulate_from_feed(&msgs);
                                game_renderer.update_feed(msgs, &self.settings);
                            }
                            Err(UpdateGameError::AlreadyEnded) => break 'wait,
                            Err(UpdateGameError::TargetTimeInPast) => unreachable!(),
                        }

                        // Note that we do not expect a button release to actually end the game or similar, but we handle things properly anyway.
                        let input = Input::Deactivate(button);

                        raw_input_history.inputs.push((update_target_time, input));

                        let update_result = game.update(update_target_time, Some(input));

                        match update_result {
                            Ok(msgs) => {
                                temp_statistics.accumulate_from_feed(&msgs);
                                game_renderer.update_feed(msgs, &self.settings)
                            }
                            Err(UpdateGameError::AlreadyEnded) => break 'wait,
                            Err(UpdateGameError::TargetTimeInPast) => unreachable!(),
                        }
                    }
                }

                // Process input as non-tetro game input action.
                match event {
                    event::Event::Key(KeyEvent {
                        code,
                        modifiers,
                        kind,
                        state: _,
                    }) => {
                        if !matches!(kind, KeyEventKind::Press | KeyEventKind::Repeat) {
                            // It just so happens that, once we're done considering in-game-relevant presses,
                            // for the remaining controls we don't care about releases.
                            continue 'wait;
                        }

                        match (code, modifiers) {
                            // [Esc]: Pause.
                            (KeyCode::Esc, _) => {
                                break 'update_and_render MenuUpdate::Push(Menu::Pause);
                            }

                            // [Ctrl+C]: Quit program.
                            (KeyCode::Char('c' | 'C'), KeyModifiers::CONTROL) => {
                                break 'update_and_render MenuUpdate::Push(Menu::Quit);
                            }

                            // Keybinds help menu.
                            (KeyCode::Char('?'), _) => {
                                let client_menu_name = "Live Game";
                                let legend = vec![
                                    (
                                        "Normal keybinds".to_owned(),
                                        [
                                            ("Esc", "Open Pause menu"),
                                            ("?", "Open Keybinds overview"),
                                        ]
                                        .into_iter()
                                        .map(|(lhs, rhs)| (lhs.to_owned(), rhs.to_owned()))
                                        .collect(),
                                    ),
                                    (
                                        "Special keybinds".to_owned(),
                                        [
                                            ("Ctrl+D", "Forfeit game"),
                                            (
                                                "Ctrl+R",
                                                "Restart game mode (overwrites current game!)",
                                            ),
                                            (
                                                "Ctrl+Z",
                                                "Undo last input (overwrites current game!)",
                                            ),
                                            ("Ctrl+L", "Load game save (overwrites current game!)"),
                                            ("Ctrl+S", "Store game save"),
                                            ("Ctrl+E", "Store seed for custom game"),
                                            (
                                                "Ctrl+G/Ctrl+Alt+G",
                                                "Cycle forward/backward through Graphics slots",
                                            ),
                                            ("Ctrl+C", "Quit program (respects save preferences)"),
                                            (
                                                "Ctrl+Alt+S",
                                                "Perform savefile store (respects save preferences)",
                                            ),
                                            (
                                                "Ctrl+Alt+L",
                                                "Reload app from savefile (overwrites current data!)",
                                            ),
                                        ]
                                        .into_iter()
                                        .map(|(lhs, rhs)| (lhs.to_owned(), rhs.to_owned()))
                                        .collect(),
                                    ),
                                    (
                                        "Player keybinds".to_owned(),
                                        Button::VARIANTS
                                            .into_iter()
                                            .map(|b| {
                                                (
                                                    format!("{b:?}"),
                                                    fmt_button_keybinds(
                                                        b,
                                                        self.settings.keybinds(),
                                                        " ",
                                                    ),
                                                )
                                            })
                                            .collect(),
                                    ),
                                ];

                                break 'update_and_render MenuUpdate::Push(
                                    Menu::KeybindsOverview {
                                        client_menu_name,
                                        legend,
                                    },
                                );
                            }

                            // [Ctrl+D]: Forfeit game.
                            (KeyCode::Char('d' | 'D'), KeyModifiers::CONTROL) => {
                                match game.forfeit() {
                                    Ok(msgs) => {
                                        temp_statistics.accumulate_from_feed(&msgs);
                                        game_renderer.update_feed(msgs, &self.settings);
                                    }

                                    // We do not care if game ended or time is in past here.
                                    Err(
                                        UpdateGameError::AlreadyEnded
                                        | UpdateGameError::TargetTimeInPast,
                                    ) => {}
                                }

                                break 'wait;
                            }

                            // [Ctrl+S]: Store game save.
                            (KeyCode::Char('s' | 'S'), KeyModifiers::CONTROL) => {
                                self.game_saves.selected = 0;
                                self.game_saves.slots = vec![GameSave {
                                    game_meta_data: game_meta_data.clone(),
                                    game_restoration_data: GameRestorationData::new(
                                        game,
                                        raw_input_history.clone(),
                                        matches!(
                                            game.phase(),
                                            Phase::GameEnd {
                                                cause: GameEndCause::Forfeit { .. },
                                                ..
                                            }
                                        )
                                        .then_some(game.state().time),
                                    ),
                                    inputs_to_load: raw_input_history.inputs.len(),
                                }];

                                game_renderer.update_feed(
                                    [(
                                        Notification::Custom("(Game save stored)".to_owned()),
                                        game.state().time,
                                    )],
                                    &self.settings,
                                );
                            }

                            // [Ctrl+L]: Load game save.
                            (KeyCode::Char('l' | 'L'), KeyModifiers::CONTROL) => {
                                if let Some(GameSave {
                                    game_meta_data: saved_meta_data,
                                    game_restoration_data,
                                    inputs_to_load,
                                }) = &self.game_saves.get()
                                {
                                    *game = game_restoration_data.restore(*inputs_to_load);

                                    *game_meta_data = saved_meta_data.clone();
                                    increment_game_mode_derivative(&mut game_meta_data.title);

                                    raw_input_history.inputs = game_restoration_data
                                        .input_history
                                        .inputs
                                        .iter()
                                        .take(*inputs_to_load)
                                        .copied()
                                        .collect();

                                    game_renderer.reset_veffects_state();
                                    game_renderer.update_feed(
                                        [(
                                            Notification::Custom("(Game save loaded)".to_owned()),
                                            game.state().time,
                                        )],
                                        &self.settings,
                                    );

                                    // What we do here is rather unholy, so we have to adapt the game loop state itself.
                                    self.statistics.play_time += Instant::now()
                                        .saturating_duration_since(time_game_loop_entered);

                                    ingametime_when_game_loop_entered = game.state().time;
                                    time_game_loop_entered = Instant::now();
                                }
                            }

                            // [Ctrl+Z]: Undo last input.
                            (KeyCode::Char('z' | 'Z'), KeyModifiers::CONTROL) => {
                                // Try to pop away latest input.
                                if let Some(mut latest_timed_input) = raw_input_history.inputs.pop()
                                {
                                    // Here we pop away *additional inputs* that happen at the *exact same time*.
                                    // This is more ergonomic for the case where an unenhanced terminal does not distinguish
                                    // between 'press' and 'release' and instead emulated them with press and release happening
                                    // at the exact same time. If user really need the distinction, the should just use the precise
                                    // input history selection for game saves in the 'Start New Game' menu.
                                    while let Some(second_latest_timed_input) =
                                        raw_input_history.inputs.last()
                                    {
                                        if second_latest_timed_input.0 != latest_timed_input.0 {
                                            break;
                                        }
                                        latest_timed_input = *second_latest_timed_input;
                                        raw_input_history.inputs.pop();
                                    }

                                    // Manually restore game to previous state.
                                    let (builder, mod_ids_cfgs) = game.reproduce_builder();
                                    let game_restoration_data = GameRestorationData {
                                        builder,
                                        mod_ids_cfgs,
                                        input_history: raw_input_history.clone(),
                                        forfeit: None,
                                    };
                                    *game = game_restoration_data
                                        .restore(game_restoration_data.input_history.inputs.len());

                                    // Mark undo as such.
                                    increment_game_mode_derivative(&mut game_meta_data.title);

                                    game_renderer.reset_veffects_state();
                                    game_renderer.update_feed(
                                        [(
                                            Notification::Custom(format!(
                                                "(Undo '{}')",
                                                match latest_timed_input.1 {
                                                    Input::Activate(button) =>
                                                        format!("{button:?} press"),
                                                    Input::Deactivate(button) =>
                                                        format!("{button:?} release"),
                                                }
                                            )),
                                            game.state().time,
                                        )],
                                        &self.settings,
                                    );

                                    // What we do here is rather unholy, so we have to adapt the game loop state itself.
                                    self.statistics.play_time += Instant::now()
                                        .saturating_duration_since(time_game_loop_entered);

                                    ingametime_when_game_loop_entered = game.state().time;
                                    time_game_loop_entered = Instant::now();
                                }
                            }

                            // [Ctrl+E]: Store seed.
                            (KeyCode::Char('e' | 'E'), KeyModifiers::CONTROL) => {
                                self.settings.game_mode_preferences.custom_config.seed =
                                    Some(game.state_init().seed);

                                game_renderer.update_feed(
                                    [(
                                        Notification::Custom(format!(
                                            "(Seed stored: {})",
                                            game.state_init().seed
                                        )),
                                        game.state().time,
                                    )],
                                    &self.settings,
                                );
                            }

                            // [Ctrl+R]: Retry game.
                            (KeyCode::Char('r' | 'R'), KeyModifiers::CONTROL) => {
                                if let Some(selection) = selection_id_for_game_retry {
                                    let game_menu = self.create_game_menu(selection);
                                    break 'update_and_render MenuUpdate::Push(game_menu);
                                }
                            }

                            // [Ctrl+Alt+B]: (Un-)Blindfold.
                            (KeyCode::Char('b' | 'B'), _)
                                if {
                                    modifiers
                                        .contains(KeyModifiers::CONTROL.union(KeyModifiers::ALT))
                                } =>
                            {
                                self.temp_data.blindfold_game ^= true;
                                if self.temp_data.blindfold_game {
                                    game_renderer.update_feed(
                                        [(
                                            Notification::Custom(
                                                "Blindfolded! [Ctrl+Alt+B]".to_owned(),
                                            ),
                                            game.state().time,
                                        )],
                                        &self.settings,
                                    );
                                } else {
                                    game_renderer.update_feed(
                                        [(
                                            Notification::Custom(
                                                "Blindfolds removed [Ctrl+Alt+B]".to_owned(),
                                            ),
                                            game.state().time,
                                        )],
                                        &self.settings,
                                    );
                                }
                            }

                            // [Ctrl(+Alt)+G]: Cycle graphics slots.
                            (KeyCode::Char('g' | 'G'), _)
                                if { modifiers.contains(KeyModifiers::CONTROL) } =>
                            {
                                self.settings.graphics_selected +=
                                    if modifiers.contains(KeyModifiers::ALT) {
                                        self.settings.graphics_slotmachine.slots.len() - 1
                                    } else {
                                        1
                                    };
                                self.settings.graphics_selected %=
                                    self.settings.graphics_slotmachine.slots.len();
                                let msg = format!(
                                    "(Graphics: {})",
                                    self.settings
                                        .graphics_slotmachine
                                        .grab(self.settings.graphics_selected)
                                        .0
                                );
                                game_renderer.update_feed(
                                    [(Notification::Custom(msg), game.state().time)],
                                    &self.settings,
                                );
                                game_renderer.reset_viewport_state(
                                    (0, 0),
                                    terminal::size().unwrap_or_default(),
                                    TermCell {
                                        bg: self.settings.tui_coloring().bg_tui,
                                        ..TermCell::BLANK
                                    },
                                );
                            }

                            // [Ctrl+Alt+L]: Reload app from savefile.
                            (KeyCode::Char('l' | 'L'), _)
                                if {
                                    modifiers
                                        .contains(KeyModifiers::CONTROL.union(KeyModifiers::ALT))
                                } =>
                            {
                                let msg = if let Err(e) = self.savefile_load() {
                                    format!("(Error reloading from savefile: {e}")
                                } else {
                                    "(Reloaded from savefile.)".to_owned()
                                };
                                game_renderer.update_feed(
                                    [(Notification::Custom(msg), game.state().time)],
                                    &self.settings,
                                );
                            }

                            // [Ctrl+Alt+S]: Store to savefile.
                            (KeyCode::Char('s' | 'S'), _)
                                if {
                                    modifiers
                                        .contains(KeyModifiers::CONTROL.union(KeyModifiers::ALT))
                                } =>
                            {
                                let msg = if let Err(e) = self.savefile_store() {
                                    format!("(Error on savefile store: {e}")
                                } else {
                                    "(Savefile store done.)".to_owned()
                                };
                                game_renderer.update_feed(
                                    [(Notification::Custom(msg), game.state().time)],
                                    &self.settings,
                                );
                            }

                            // Other misc. key event: We don't care.
                            _ => {}
                        }
                    }

                    // Auto-pause on focus lost.
                    event::Event::FocusLost => {
                        if self.temp_data.pause_on_focus_lost {
                            break 'update_and_render MenuUpdate::Push(Menu::Pause);
                        }
                    }

                    event::Event::FocusGained => {}
                    event::Event::Mouse(_) => {}
                    event::Event::Paste(_) => {}
                    event::Event::Resize(cols, rows) => {
                        // Need to redraw screen for proper centering etc.
                        game_renderer.reset_viewport_state(
                            (0, 0),
                            (cols, rows),
                            TermCell {
                                bg: self.settings.tui_coloring().bg_tui,
                                ..TermCell::BLANK
                            },
                        );
                        break 'wait;
                    }
                }
            }

            let now = Instant::now();

            // We first calculate the intended time at time of reaching here.
            let update_target_time = ingametime_when_game_loop_entered
                + now.saturating_duration_since(time_game_loop_entered);

            match game.update(update_target_time, None) {
                // Update.
                Ok(msgs) => {
                    temp_statistics.accumulate_from_feed(&msgs);
                    game_renderer.update_feed(msgs, &self.settings)
                }

                // We do not care if game ended or time is in past here:
                // We just care about best-effort updating state to show it to player.
                Err(UpdateGameError::AlreadyEnded | UpdateGameError::TargetTimeInPast) => {}
            }

            // Render current state of the game.
            game_renderer.render(
                &mut self.term,
                game,
                game_meta_data,
                &self.settings,
                &self.temp_data,
                &keybinds_legend,
                None,
            )?;

            renders_per_second_counter += 1;

            // Render FPS counter.
            if self.settings.graphics().show_fps {
                let secs_diff = now
                    .saturating_duration_since(renders_per_second_counter_start_time)
                    .as_secs_f64();
                const REFRESH_FPS_COUNTER_EVERY_N_SECS: f64 = 1.0;

                // One second has passed since we started counting.
                if secs_diff >= REFRESH_FPS_COUNTER_EVERY_N_SECS {
                    self.term.execute(MoveTo(0, 0))?;
                    self.term.execute(Print(format!(
                        "{:_>7}",
                        format!(
                            "{:.1}fps",
                            f64::from(renders_per_second_counter) / secs_diff
                        )
                    )))?;

                    // Reset data.
                    renders_per_second_counter = 0;
                    renders_per_second_counter_start_time = now;
                }
            }
        };

        // Game loop epilogue: De-initialization.

        if self.temp_data.kitty_assumed {
            // FIXME: Explicitly ignore an error when pushing flags. This is so we can still try even if Crossterm doesn't like operating on Windows.
            let _r = self.term.execute(event::PopKeyboardEnhancementFlags);
        }

        if !game.has_ended() {
            // Game not done:.
            // Manually release any pressed buttons to avoid weird persistent-buttonpress behavior.
            let unpress_time = game.state().time.quantize();
            'button_unpressing: for button in Button::VARIANTS {
                if game.state().active_buttons[button].is_some() {
                    let input = Input::Deactivate(button);

                    let update_result = game.update(unpress_time, Some(input));

                    raw_input_history.inputs.push((unpress_time, input));
                    match update_result {
                        Ok(msgs) => {
                            temp_statistics.accumulate_from_feed(&msgs);
                            game_renderer.update_feed(msgs, &self.settings);
                        }
                        Err(UpdateGameError::AlreadyEnded) => break 'button_unpressing,
                        Err(UpdateGameError::TargetTimeInPast) => unreachable!(),
                    }
                }
            }
        }

        self.statistics.play_time +=
            Instant::now().saturating_duration_since(time_game_loop_entered);

        if !Statistics::GAME_MODE_TITLE_PREFIX_BLACKLIST
            .iter()
            .any(|prefix| game_meta_data.title.starts_with(prefix))
        {
            self.statistics.accumulate(&temp_statistics);
        }

        Ok(menu_update)
    }
}

mod input_catcher {
    use std::{
        sync::mpsc::{SendError, Sender},
        thread::{self, JoinHandle},
        time::Instant,
    };

    use crossterm::event::{self, Event};

    pub fn spawn(
        input_sender: Sender<(Event, Instant)>,
        is_stop_event: impl Fn(Event) -> bool + Send + 'static,
    ) -> JoinHandle<()> {
        thread::spawn(move || {
            'detect_events: loop {
                // Read event.
                match event::read() {
                    Ok(event) => {
                        let timestamp = Instant::now();

                        // Send signal.
                        match input_sender.send((event.clone(), timestamp)) {
                            Ok(()) => {}
                            Err(SendError(_event_which_failed_to_transmit)) => {
                                break 'detect_events;
                            }
                        }

                        if is_stop_event(event) {
                            break 'detect_events;
                        }
                    }

                    // FIXME: Handle io::Error? If not, why not?
                    Err(_e) => {}
                }
            }
        })
    }
}