trw 0.0.3

3D Trapping Rain Water visualization demo
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
use clap::Parser;
use crossterm::{
    cursor::{Hide, Show},
    event::{
        self, Event, KeyCode, KeyEventKind, KeyModifiers, KeyboardEnhancementFlags,
        PopKeyboardEnhancementFlags, PushKeyboardEnhancementFlags,
    },
    execute,
    terminal::{EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode, enable_raw_mode},
};

use ratatui::{
    buffer::Buffer,
    layout::Rect,
    widgets::{Block, Borders, Paragraph, Widget},
};
use ratatui_wireframe::model::Model;

use std::{
    collections::HashSet,
    error::Error,
    io::{self},
    sync::mpsc::{self},
    thread,
    time::{Duration, Instant},
};

// Standard TRW algorithm: used only to snapshot the deterministic equilibrium exactly at convergence (not part of any UI/key handling)
mod algorithm;
mod particle;
mod physics;
mod render;
mod terrain;
mod view;
mod water;

use particle::ParticleSystem;
use physics::Physics;
use terrain::Terrain;
use view::ViewState;

// Wireforge constants
const ROT_RATE: f64 = 169.0 / 128.0;
const MOVE_RATE: f64 = 83.0 / 128.0;
const SPIN_RATE: f64 = 169.0 / 256.0;
/// How often the idle loop re-checks the terminal size (resize fallback; crossterm
/// also delivers Event::Resize directly, this poll is only for terminals that don't).
const RESIZE_CHECK_INTERVAL: Duration = Duration::from_millis(500);

/// Motion state
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum Motion {
    YawLeft,
    YawRight,
    PitchUp,
    PitchDown,
    RollPlus,
    RollMinus,
    MoveLeft,
    MoveRight,
    MoveUp,
    MoveDown,
    MoveForward,
    MoveBack,
}

/// Motion input state
/// Map key to motion
fn motion_for(code: KeyCode, shift: bool) -> Option<Motion> {
    use KeyCode::*;
    Some(match code {
        Left if shift => Motion::MoveLeft,
        Right if shift => Motion::MoveRight,
        Up if shift => Motion::MoveUp,
        Down if shift => Motion::MoveDown,
        Char('h') | Char('H') if shift => Motion::MoveLeft,
        Char('l') | Char('L') if shift => Motion::MoveRight,
        Char('k') | Char('K') if shift => Motion::MoveUp,
        Char('j') | Char('J') if shift => Motion::MoveDown,
        Char('=') | Char('+') => Motion::MoveForward,
        Char('-') => Motion::MoveBack,
        Left | Char('h') => Motion::YawLeft,
        Right | Char('l') => Motion::YawRight,
        Up | Char('k') => Motion::PitchUp,
        Down | Char('j') => Motion::PitchDown,
        Char('r') => Motion::RollPlus,
        Char('e') => Motion::RollMinus,
        _ => return None,
    })
}

/// HUD fold state
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Hud {
    Collapsed,
    Expanded,
}

/// Rendering engine state
struct Engine {
    screen: render_impl::Screen,
    raster: render_impl::Rasterizer,
    hud_buf: Option<Buffer>,
}

impl Engine {
    fn new(w: usize, h: usize) -> Self {
        Engine {
            screen: render_impl::Screen::new(w, h),
            raster: render_impl::Rasterizer::new(),
            hud_buf: None,
        }
    }

    fn resize(&mut self, w: usize, h: usize) {
        self.screen.resize(w, h);
        self.hud_buf = None;
    }
}

/// Application state
struct App {
    // View state
    view: ViewState,
    held: HashSet<Motion>,
    auto_spin: bool,
    hud: Hud,
    show_axes: bool,
    dirty: bool,
    terrain_model: Model,
    water_model: Model,

    // Project-specific state
    terrain: Terrain,
    particles: ParticleSystem,
    physics: Physics,
    /// Rotation center (container center = (n/2, n/2, n/2))
    center: [f64; 3],
    paused: bool,
    raining: bool,
    trapped_water: usize,
    /// X-Ray mode: see-through display of rain particles hidden behind the terrain
    xray: bool,
    /// Whether water levels have converged after rain stops (physics frozen; trapped stays stable)
    settled: bool,
    grid_size: usize,
}

impl App {
    fn new(grid_size: usize) -> Self {
        let terrain = Terrain::new(grid_size, grid_size);
        let particles = ParticleSystem::new();
        let physics = Physics::new(grid_size, grid_size);
        let terrain_model = render::build_terrain_model(&terrain);
        let water_model = render::build_water_model(&physics.water, &terrain, &particles);
        // rotation center = container center (n/2, n/2, n/2)
        let center = [grid_size as f64 / 2.0; 3];
        let view = ViewState {
            center,
            ..ViewState::default()
        };
        App {
            view,
            held: HashSet::new(),
            auto_spin: false,
            hud: Hud::Collapsed,
            show_axes: true,
            dirty: true,
            terrain_model,
            water_model,
            terrain,
            particles,
            physics,
            center,
            paused: false,
            raining: false,
            trapped_water: 0,
            xray: false,
            settled: false,
            grid_size,
        }
    }

    /// Rebuild only the water model (called whenever levels/particles change per frame; no need if terrain is unchanged)
    fn rebuild_water_model(&mut self) {
        self.water_model =
            render::build_water_model(&self.physics.water, &self.terrain, &self.particles);
    }

    /// Rebuild the terrain model (called only when the terrain is regenerated)
    fn rebuild_terrain_model(&mut self) {
        self.terrain_model = render::build_terrain_model(&self.terrain);
    }

    /// Generate random terrain
    ///
    /// Column heights scale with the grid size: max height = grid_size - 1,
    /// so a larger grid also gets taller terrain (the default 8 keeps the
    /// original 0..=7 range).
    fn generate_terrain(&mut self) {
        self.terrain
            .generate_random(self.grid_size.saturating_sub(1) as u8);
        self.particles.clear();
        self.physics.clear();
        self.trapped_water = 0;
        self.settled = false;
        self.rebuild_terrain_model();
        self.rebuild_water_model();
        self.dirty = true;
    }

    /// Toggle rain
    fn toggle_rain(&mut self) {
        self.raining = !self.raining;
        self.settled = false;
        self.dirty = true;
    }

    /// Toggle pause (pause/resume the whole physics simulation)
    fn toggle_pause(&mut self) {
        self.paused = !self.paused;
        self.dirty = true;
    }

    /// Reset the view: restore the default camera but keep the rotation center (container center)
    fn reset_view(&mut self) {
        self.view = ViewState::default();
        self.view.center = self.center;
        self.view.fit_to(view::model_extent(&self.terrain_model));
        self.dirty = true;
    }

    /// Clear particles
    fn clear_particles(&mut self) {
        self.particles.clear();
        self.physics.clear();
        self.trapped_water = 0;
        self.settled = false;
        self.rebuild_water_model();
        self.dirty = true;
    }

    /// Rain: accumulate rainfall by real dt (independent of frame rate; random position is the allowed random source)
    fn spawn_particles(&mut self, dt: f64) {
        if !self.raining || self.paused || self.settled {
            return;
        }
        self.physics
            .add_rain(dt, &mut self.particles, &self.terrain);
    }

    /// Update physics with real dt
    fn update_physics(&mut self, dt: f64) {
        if self.paused || self.settled {
            return;
        }
        self.physics.update(&mut self.particles, &self.terrain, dt);
        self.particles
            .remove_out_of_bounds(self.grid_size as f64, self.grid_size as f64, -5.0);
        // live trapped-water count (units: number of 1x1x1 cubes)
        self.trapped_water = self.physics.total_water_units().round() as usize;

        // after rain stops: wait until flow and drainage mostly stop and all particles settle -> converge and freeze
        // flow rules are deterministic and the converged value depends only on the terrain -> same container, same result
        if !self.raining {
            // guard: never converge when no rain has fallen / no water exists (avoid misjudging dry land)
            let has_water = self.physics.water.total_water() > 0.0;
            let no_more_flow = self.physics.converged(dt);
            let all_settled = !self
                .particles
                .particles
                .iter()
                .any(|p| (p.z - p.target_z).abs() > 0.01);
            if has_water && no_more_flow && all_settled {
                let total = self.physics.total_water_units();
                let answer = self.physics.equilibrium_units(&self.terrain);
                if (total - answer as f64).abs() <= 1.0 {
                    // saturated: snapshot exactly to the unique deterministic equilibrium (result independent of the rain path)
                    self.trapped_water = self.physics.finalize(&mut self.particles, &self.terrain);
                } else {
                    // not saturated: freeze at the current converged value
                    self.trapped_water = total.round() as usize;
                }
                self.settled = true;
            }
        }
    }

    /// Handle input event; returns true to quit
    fn handle_input(&mut self, ev: Event) -> bool {
        if let Event::Key(key) = ev {
            let shift = key.modifiers.contains(KeyModifiers::SHIFT);
            // Kitty keyboard protocol: a Release event stops the motion immediately.
            if key.kind == KeyEventKind::Release {
                if let Some(m) = motion_for(key.code, shift) {
                    self.held.remove(&m);
                    self.dirty = true;
                }
                return false;
            }
            match key.code {
                KeyCode::Char('q') | KeyCode::Esc => return true,
                KeyCode::Char('?') => {
                    self.hud = match self.hud {
                        Hud::Collapsed => Hud::Expanded,
                        Hud::Expanded => Hud::Collapsed,
                    };
                    self.dirty = true;
                }
                KeyCode::Tab => {
                    self.show_axes = !self.show_axes;
                    self.dirty = true;
                }
                KeyCode::Char(' ') => {
                    self.auto_spin = !self.auto_spin;
                    self.dirty = true;
                }
                KeyCode::Char('f') | KeyCode::Char('F') if shift => {
                    self.view.fit_to(view::model_extent(&self.terrain_model));
                    self.dirty = true;
                }
                KeyCode::Char('f') | KeyCode::Char('F') => {
                    self.view.center_origin();
                    self.dirty = true;
                }
                KeyCode::Char('0') => {
                    self.reset_view();
                }
                // Project-specific keys
                KeyCode::Char('g') => {
                    self.generate_terrain();
                }
                KeyCode::Char('w') => {
                    self.toggle_rain();
                }
                KeyCode::Char('p') => {
                    self.toggle_pause();
                }
                KeyCode::Char('c') => {
                    self.clear_particles();
                }
                KeyCode::Char('x') => {
                    self.xray = !self.xray;
                    self.dirty = true;
                }
                // Motion keys
                _ => {
                    if let Some(m) = motion_for(key.code, shift) {
                        // Press/Repeat: the key is held. Continuous motion is applied by
                        // update_held each frame with dt; Release is handled above.
                        self.held.insert(m);
                        self.dirty = true;
                    }
                }
            }
        }
        false
    }

    /// Update held motion state
    /// Apply continuous motion to every held key each frame (dt-scaled).
    /// Keys are added on Press/Repeat and removed on Release (Kitty protocol).
    fn update_held(&mut self, dt: f64) {
        if self.held.is_empty() {
            return;
        }
        let ms = view::model_extent(&self.terrain_model);
        for m in &self.held {
            continuous_step(&mut self.view, *m, ms, dt);
        }
        // Held keys need a redraw every frame, otherwise the continuous motion
        // is applied but never shown until the next key event.
        self.dirty = true;
    }
}

/// Continuous held step
fn continuous_step(v: &mut ViewState, m: Motion, scale: f64, dt: f64) {
    apply_motion_step(v, m, ROT_RATE * dt, scale * MOVE_RATE * dt);
}

/// Apply motion step
fn apply_motion_step(v: &mut ViewState, m: Motion, rot: f64, mv: f64) {
    match m {
        Motion::YawLeft => v.add_yaw(rot),
        Motion::YawRight => v.add_yaw(-rot),
        Motion::PitchUp => v.add_pitch(-rot),
        Motion::PitchDown => v.add_pitch(rot),
        Motion::RollPlus => v.roll -= rot,
        Motion::RollMinus => v.roll += rot,
        Motion::MoveLeft => v.pan_x -= mv,
        Motion::MoveRight => v.pan_x += mv,
        Motion::MoveUp => v.pan_y += mv,
        Motion::MoveDown => v.pan_y -= mv,
        Motion::MoveForward => v.add_dist_delta(-mv),
        Motion::MoveBack => v.add_dist_delta(mv),
    }
    v.normalize();
}

/// HUD layout helper: produce the status lines
fn hud_layout(app: &App) -> (String, String) {
    let mut row0 = format!(
        "TRW | yaw={:.2} pitch={:.2} roll={:.2} dist={:.2} pan=({:.2},{:.2})",
        app.view.yaw, app.view.pitch, app.view.roll, app.view.dist, app.view.pan_x, app.view.pan_y
    );

    // Wireforge style: show the question-mark hint only when collapsed
    if app.hud == Hud::Collapsed {
        row0.push_str("   [?] keys");
    }

    let paused_str = if app.paused { "YES" } else { "NO" };
    let xray_str = if app.xray { "YES" } else { "NO" };
    let row1 = format!(
        "Water: {} particles | paused: {} | xray: {} | trapped: {} units",
        app.particles.count(),
        paused_str,
        xray_str,
        app.trapped_water
    );

    (row0, row1)
}

/// Copy rows [y0, y1) of the HUD buffer into the screen
fn blit_hud_rows(screen: &mut render_impl::Screen, hud: &Buffer, y0: u16, y1: u16) {
    let (w, _) = screen.size();
    for y in y0..y1 {
        for x in 0..w {
            let cell = &hud[(x as u16, y)];
            let ch = cell.symbol().chars().next().unwrap_or(' ');
            screen.set(x, y as usize, ch, render_impl::color_idx(cell.fg));
        }
    }
}

/// The help overlay text
const HELP: &[&str] = &[
    "=== keys ===",
    "",
    "Rotate:",
    "  yaw left  ← / h        yaw right  → / l",
    "  pitch up  ↑ / k         pitch down ↓ / j",
    "  roll      r / e",
    "",
    "Move:",
    "  left      Shift+← / h  right      Shift+→ / l",
    "  up        Shift+↑ / k   down       Shift+↓ / j",
    "  nearer    =             farther    -",
    "",
    "Keys:",
    "  center    f             fit        Shift+f",
    "  reset     0             spin       Space",
    "  keys      ?             axes       Tab",
    "  generate  g             water      w",
    "  pause     p             xray       x",
    "  clear     c             quit       q / Esc",
    "",
    "[?] close help",
];

/// Render one frame
fn render_frame(
    app: &mut App,
    engine: &mut Engine,
    stdout: &mut io::Stdout,
) -> Result<(), Box<dyn Error>> {
    let (w, h) = engine.screen.size();
    if w == 0 || h == 0 {
        return Ok(());
    }
    let w16 = w as u16;
    let h16 = h as u16;

    let (row0, row1) = hud_layout(app);

    // HUD rows: row0 + row1 = 2 rows, or more if help is shown
    let overlay: Option<Vec<String>> = if app.hud == Hud::Expanded {
        Some(HELP.iter().map(|s| s.to_string()).collect())
    } else {
        None
    };

    // Reallocate HUD buffer if needed
    let needs_realloc = engine
        .hud_buf
        .as_ref()
        .is_none_or(|b| b.area.width != w16 || b.area.height != h16);
    if needs_realloc {
        engine.hud_buf = Some(Buffer::empty(Rect::new(0, 0, w16, h16)));
    } else if let Some(b) = engine.hud_buf.as_mut() {
        b.reset();
    }

    // Draw HUD rows
    let canvas_top: u16;
    {
        let hud = engine.hud_buf.as_mut().unwrap();
        Paragraph::new(row0).render(Rect::new(0, 0, w16, 1), hud);
        Paragraph::new(row1).render(Rect::new(0, 1, w16, 1), hud);
        canvas_top = 2;
    }

    // Draw overlay or model
    let canvas_area = if let Some(lines) = overlay {
        let top = canvas_top;
        let height = h16.saturating_sub(top);
        let area = Rect::new(0, top, w16, height);
        let block = Block::default().borders(Borders::ALL);
        let inner = block.inner(area);
        let hud = engine.hud_buf.as_mut().unwrap();
        block.render(area, hud);
        for (i, line) in lines.iter().enumerate() {
            if i as u16 >= inner.height {
                break;
            }
            Paragraph::new(line.as_str())
                .render(Rect::new(inner.x, inner.y + i as u16, inner.width, 1), hud);
        }
        None
    } else {
        Some(Rect::new(
            0,
            canvas_top,
            w16,
            h16.saturating_sub(canvas_top),
        ))
    };

    // Blit HUD to screen
    {
        let screen = &mut engine.screen;
        let hud = engine.hud_buf.as_ref().unwrap();
        blit_hud_rows(screen, hud, 0, canvas_top);
    }

    if let Some(area) = canvas_area {
        let cw = area.width as usize;
        let ch = area.height as usize;
        if cw > 0 && ch > 0 {
            engine.raster.resize(cw, ch);

            // render the terrain (uses the terminal default foreground; resetting the depth buffer = a new frame)
            engine.raster.render_colored(
                &app.terrain_model,
                &app.view,
                render_impl::RenderOpts {
                    region: (0, canvas_top as usize, cw, ch),
                    show_axes: app.show_axes,
                    edge_color: render_impl::Fg::Default as u8, // terminal default foreground
                    keep_depth: false,
                },
                &mut engine.screen,
            );

            // render the water (blue columns + falling drops):
            // - normal mode: keep the terrain depth buffer -> water columns are occluded by the terrain
            // - X-Ray mode: reset the depth buffer -> see-through (drawn above the terrain)
            engine.raster.render_colored(
                &app.water_model,
                &app.view,
                render_impl::RenderOpts {
                    region: (0, canvas_top as usize, cw, ch),
                    show_axes: false, // no axes (shown only when rendering the terrain)
                    edge_color: render_impl::Fg::LightBlue as u8, // blue water columns/drops
                    keep_depth: !app.xray,
                },
                &mut engine.screen,
            );
        }
    } else {
        // Overlay: blit the rest
        let screen = &mut engine.screen;
        let hud = engine.hud_buf.as_ref().unwrap();
        blit_hud_rows(screen, hud, canvas_top, h16);
    }

    // Present
    engine.screen.present(stdout)?;
    Ok(())
}

/// Command-line arguments
#[derive(Parser, Debug)]
#[command(
    name = "trw",
    author,
    version,
    about = "3D Trapping Rain Water visualization demo"
)]
struct Args {
    /// Grid size
    #[arg(short, long, default_value = "8")]
    grid_size: usize,
}

fn main() -> Result<(), Box<dyn Error>> {
    let args = Args::parse();
    let grid_size = args.grid_size;

    enable_raw_mode()?;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen, Hide)?;
    // Enable the Kitty keyboard protocol for Press/Repeat/Release key events.
    // Keyboard enhancement (kitty protocol) is optional: terminals that don't
    // support it (e.g. the legacy Windows console API) run without it.
    let _ = execute!(
        stdout,
        PushKeyboardEnhancementFlags(
            KeyboardEnhancementFlags::REPORT_EVENT_TYPES
                | KeyboardEnhancementFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES
                | KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES
        )
    );

    let (tx, rx) = mpsc::channel::<Event>();

    // Input thread
    thread::spawn(move || {
        loop {
            if let Ok(ev) = event::read()
                && tx.send(ev).is_err()
            {
                break;
            }
        }
    });

    let mut app = App::new(grid_size);
    app.generate_terrain();
    app.view.fit_to(view::model_extent(&app.terrain_model));

    let (cols, rows) = crossterm::terminal::size()?;
    let mut engine = Engine::new(cols as usize, rows as usize);

    // Render initial frame
    render_frame(&mut app, &mut engine, &mut stdout)?;
    app.dirty = false;

    let mut last = Instant::now();

    // Two-mode loop (Wireforge-style): animating = drain + update + render as
    // fast as possible; idle = block in the channel until an event or the
    // periodic resize fallback wakes us (the thread is parked in the kernel,
    // 0% CPU). This keeps the loop from pegging every core when nothing moves.
    'main: loop {
        let now = Instant::now();
        let dt = (now - last).as_secs_f64().min(0.1);
        last = now;

        // True when the next frame can change something (view motion, live
        // physics, or rain). Settled water is frozen, so it is NOT animating.
        let physics_active = (app.raining
            || !app.particles.particles.is_empty()
            || app.physics.water.total_water() > 0.0)
            && !app.paused
            && !app.settled;
        let animating = app.auto_spin || !app.held.is_empty() || physics_active;

        if animating {
            // Handle resize
            if let Ok((new_cols, new_rows)) = crossterm::terminal::size()
                && (new_cols as usize != engine.screen.size().0
                    || new_rows as usize != engine.screen.size().1)
            {
                engine.resize(new_cols as usize, new_rows as usize);
                app.dirty = true;
            }

            // Drain input
            while let Ok(ev) = rx.try_recv() {
                if let Event::Resize(cols, rows) = ev {
                    engine.resize(cols as usize, rows as usize);
                    app.dirty = true;
                    continue;
                }
                if app.handle_input(ev) {
                    break 'main;
                }
            }

            // Update held keys
            app.update_held(dt);

            // Auto spin
            if app.auto_spin {
                app.view.spin_local(SPIN_RATE * dt);
                app.view.normalize();
                app.dirty = true;
            }

            // Spawn rain particles
            if app.raining && !app.paused {
                app.spawn_particles(dt);
            }

            // Update physics (only when there is something to simulate; avoids
            // feeding the rayon pool with empty parallel jobs during pure view motion)
            let had_particles = !app.particles.particles.is_empty();
            let had_water = app.physics.water.total_water() > 0.0;
            if physics_active {
                app.update_physics(dt);
            }
            if !app.particles.particles.is_empty()
                || had_particles
                || app.physics.water.total_water() > 0.0
                || had_water
            {
                app.rebuild_water_model();
                app.dirty = true;
            }

            // Render if dirty
            if app.dirty {
                render_frame(&mut app, &mut engine, &mut stdout)?;
                app.dirty = false;
            }
        } else {
            // Idle: block in the channel until an input event or the resize
            // fallback — the thread is parked in the kernel (0% CPU).
            match rx.recv_timeout(RESIZE_CHECK_INTERVAL) {
                Ok(Event::Resize(cols, rows)) => {
                    engine.resize(cols as usize, rows as usize);
                    app.dirty = true;
                }
                Ok(ev) => {
                    if app.handle_input(ev) {
                        break 'main;
                    }
                }
                Err(mpsc::RecvTimeoutError::Timeout) => {
                    // Periodic resize fallback.
                    if let Ok((new_cols, new_rows)) = crossterm::terminal::size()
                        && (new_cols as usize != engine.screen.size().0
                            || new_rows as usize != engine.screen.size().1)
                    {
                        engine.resize(new_cols as usize, new_rows as usize);
                        app.dirty = true;
                    }
                }
                Err(mpsc::RecvTimeoutError::Disconnected) => break 'main,
            }
            // Redraw only when something actually changed (dirty-flag).
            if app.dirty {
                render_frame(&mut app, &mut engine, &mut stdout)?;
                app.dirty = false;
            }
        }
    }

    let _ = execute!(stdout, PopKeyboardEnhancementFlags);
    execute!(stdout, Show, LeaveAlternateScreen)?;
    disable_raw_mode()?;
    Ok(())
}

/// The Wireforge rendering engine (Screen, Rasterizer, etc.)
/// Adapted from /home/lenitain/.projects/wireforge/src/render.rs
mod render_impl {
    use ratatui::style::Color;
    use ratatui_wireframe::model::Model;
    use std::io::Write;

    use crate::view::{self, ViewState};

    const SPACE: u64 = (' ' as u64) << 8;

    #[inline(always)]
    fn pack(ch: char, fg: u8) -> u64 {
        (ch as u64) << 8 | fg as u64
    }

    #[derive(Clone, Copy, PartialEq, Eq, Debug)]
    #[repr(u8)]
    pub enum Fg {
        Default = 0,
        Cyan = 1,
        Red = 2,
        Yellow = 3,
        LightBlue = 4,
    }

    impl Fg {
        pub fn sgr(self) -> &'static [u8] {
            match self {
                Fg::Default => b"\x1b[0m",
                Fg::Cyan => b"\x1b[36m",
                Fg::Red => b"\x1b[31m",
                Fg::Yellow => b"\x1b[33m",
                Fg::LightBlue => b"\x1b[94m",
            }
        }

        pub fn from_u8(v: u8) -> Fg {
            match v {
                1 => Fg::Cyan,
                2 => Fg::Red,
                3 => Fg::Yellow,
                4 => Fg::LightBlue,
                _ => Fg::Default,
            }
        }
    }

    pub fn color_idx(c: Color) -> u8 {
        match c {
            Color::Cyan => Fg::Cyan as u8,
            Color::Red => Fg::Red as u8,
            Color::Yellow => Fg::Yellow as u8,
            Color::LightBlue => Fg::LightBlue as u8,
            _ => Fg::Default as u8,
        }
    }

    pub struct Screen {
        w: usize,
        h: usize,
        cur: Vec<u64>,
        prev: Vec<u64>,
        out: Vec<u8>,
        full_repaint: bool,
    }

    impl Screen {
        pub fn new(w: usize, h: usize) -> Self {
            let n = w * h;
            Screen {
                w,
                h,
                cur: vec![SPACE; n],
                prev: vec![SPACE; n],
                out: Vec::with_capacity(4096),
                full_repaint: false,
            }
        }

        pub fn resize(&mut self, w: usize, h: usize) {
            if w == self.w && h == self.h {
                return;
            }
            self.w = w;
            self.h = h;
            self.cur = vec![SPACE; w * h];
            self.prev = vec![SPACE; w * h];
            self.full_repaint = true;
        }

        pub fn size(&self) -> (usize, usize) {
            (self.w, self.h)
        }

        #[inline]
        pub fn set(&mut self, x: usize, y: usize, ch: char, fg: u8) {
            if x < self.w && y < self.h {
                self.cur[y * self.w + x] = pack(ch, fg);
            }
        }

        fn push_usize(out: &mut Vec<u8>, mut n: usize) {
            if n == 0 {
                out.push(b'0');
                return;
            }
            let mut tmp = [0u8; 20];
            let mut i = tmp.len();
            while n > 0 {
                i -= 1;
                tmp[i] = b'0' + (n % 10) as u8;
                n /= 10;
            }
            out.extend_from_slice(&tmp[i..]);
        }

        fn push_cursor(out: &mut Vec<u8>, row: usize, col: usize) {
            out.push(0x1b);
            out.push(b'[');
            Self::push_usize(out, row + 1);
            out.push(b';');
            Self::push_usize(out, col + 1);
            out.push(b'H');
        }

        pub fn present<W: Write>(&mut self, out: &mut W) -> std::io::Result<usize> {
            self.out.clear();
            if self.full_repaint {
                self.out.extend_from_slice(b"\x1b[2J");
                self.full_repaint = false;
            }
            let mut changed = 0;
            for y in 0..self.h {
                let base = y * self.w;
                let cur_row = &self.cur[base..base + self.w];
                let prev_row = &self.prev[base..base + self.w];
                if cur_row == prev_row {
                    continue;
                }
                let mut x = 0;
                while x < self.w {
                    if cur_row[x] == prev_row[x] {
                        x += 1;
                        continue;
                    }
                    let start = x;
                    while x < self.w && cur_row[x] != prev_row[x] {
                        x += 1;
                    }
                    let end = x;
                    Self::push_cursor(&mut self.out, y, start);
                    let mut last_fg = u8::MAX;
                    for &cell in &cur_row[start..end] {
                        let fg = (cell & 0xff) as u8;
                        if fg != last_fg {
                            self.out.extend_from_slice(Fg::from_u8(fg).sgr());
                            last_fg = fg;
                        }
                        let ch = char::from_u32((cell >> 8) as u32).unwrap_or(' ');
                        let mut b = [0u8; 4];
                        self.out
                            .extend_from_slice(ch.encode_utf8(&mut b).as_bytes());
                    }
                    changed += end - start;
                }
            }
            std::mem::swap(&mut self.cur, &mut self.prev);
            self.cur.fill(SPACE);
            out.write_all(&self.out)?;
            out.flush()?;
            Ok(changed)
        }
    }

    pub struct Rasterizer {
        cw: usize,
        ch: usize,
        dots: Vec<u8>,
        colors: Vec<u8>,
        braille: [char; 256],
        proj: Vec<[f64; 2]>,
        proj_ok: Vec<bool>,
        proj_depth: Vec<f32>,
        proj_view: ViewState,
        proj_px_w: usize,
        proj_px_h: usize,
        proj_len: usize,
        depth_buf: Vec<f32>,
    }

    impl Default for Rasterizer {
        fn default() -> Self {
            Self::new()
        }
    }

    /// Parameters for a single model render (keeps `render_colored` arguments under 7)
    pub struct RenderOpts {
        /// Canvas region (x, y, width, height)
        pub region: (usize, usize, usize, usize),
        /// Whether to show the axes (shown only when rendering the terrain)
        pub show_axes: bool,
        /// Edge color
        pub edge_color: u8,
        /// Whether to keep the depth buffer from the previous call (true = depth-test against earlier models)
        pub keep_depth: bool,
    }

    impl Rasterizer {
        pub fn new() -> Self {
            Rasterizer {
                cw: 0,
                ch: 0,
                braille: ratatui::symbols::braille::BRAILLE,
                dots: Vec::new(),
                colors: Vec::new(),
                proj: Vec::new(),
                proj_ok: Vec::new(),
                proj_depth: Vec::new(),
                proj_view: ViewState::default(),
                proj_px_w: 0,
                proj_px_h: 0,
                proj_len: 0,
                depth_buf: Vec::new(),
            }
        }

        pub fn resize(&mut self, cw: usize, ch: usize) {
            self.cw = cw;
            self.ch = ch;
            self.dots = vec![0; cw * ch];
            self.colors = vec![0; cw * ch];
            self.depth_buf = vec![f32::MAX; cw * 2 * ch * 4];
            self.proj_px_w = 0;
            self.proj_px_h = 0;
        }

        fn project(&mut self, model: &Model, view: &ViewState, px_w: usize, px_h: usize) {
            let n = model.vertices.len();
            if self.proj_len == n
                && self.proj_view == *view
                && self.proj_px_w == px_w
                && self.proj_px_h == px_h
            {
                return;
            }
            self.proj.clear();
            self.proj.resize(n, [0.0; 2]);
            self.proj_ok.clear();
            self.proj_ok.resize(n, false);
            self.proj_depth.clear();
            self.proj_depth.resize(n, f32::MAX);
            view::project_batch(
                &model.vertices,
                view,
                px_h,
                &mut self.proj,
                &mut self.proj_ok,
                &mut self.proj_depth,
            );
            self.proj_view = *view;
            self.proj_px_w = px_w;
            self.proj_px_h = px_h;
            self.proj_len = n;
        }

        /// Render a model.
        ///
        /// `keep_depth` (in `opts`): when `false`, resets the depth buffer (rendered independently,
        /// always drawn on top); when `true`, keeps the previous depth buffer so this model
        /// depth-tests against earlier models (e.g. particles keep the terrain depth buffer -> occluded by the terrain).
        pub fn render_colored(
            &mut self,
            model: &Model,
            view: &ViewState,
            opts: RenderOpts,
            screen: &mut Screen,
        ) {
            let RenderOpts {
                region,
                show_axes,
                edge_color,
                keep_depth,
            } = opts;
            let (rx, ry, cw, ch) = region;
            debug_assert!(cw > 0 && ch > 0);
            let px_w = cw * 2;
            let px_h = ch * 4;
            self.project(model, view, px_w, px_h);

            self.dots.fill(0);
            self.colors.fill(0);
            if !keep_depth {
                self.depth_buf.fill(f32::MAX);
            }

            // Fill depth buffer with box face depths for occlusion
            fill_box_faces(
                &self.proj,
                &self.proj_ok,
                &self.proj_depth,
                model.vertices.len(),
                px_w,
                px_h,
                &mut self.depth_buf,
            );

            for &(a, b) in &model.edges {
                if self.proj_ok[a] && self.proj_ok[b] {
                    let [x1, y1] = self.proj[a];
                    let [x2, y2] = self.proj[b];
                    paint_line_into(
                        x1,
                        y1,
                        x2,
                        y2,
                        self.proj_depth[a],
                        self.proj_depth[b],
                        edge_color,
                        self.cw,
                        px_w,
                        px_h,
                        &mut self.dots,
                        &mut self.colors,
                        &mut self.depth_buf,
                        true,
                    );
                }
            }

            // Axes (no depth testing — always visible)
            // axes are rooted at the rotation center (view.center = container center)
            let mut labels: Vec<(f64, f64, &str, u8)> = Vec::new();
            if show_axes {
                let axis_len = view::model_extent(model) / 0.618;
                let (cx, cy, cz) = (view.center[0], view.center[1], view.center[2]);
                let origin = view::project_point((cx, cy, cz), view, px_h);
                let ends = [
                    (
                        view::project_point((cx + axis_len, cy, cz), view, px_h),
                        "X",
                        Fg::Red as u8,
                    ),
                    (
                        view::project_point((cx, cy + axis_len, cz), view, px_h),
                        "Y",
                        Fg::Yellow as u8,
                    ),
                    (
                        view::project_point((cx, cy, cz + axis_len), view, px_h),
                        "Z",
                        Fg::LightBlue as u8,
                    ),
                ];
                if let Some((ox, oy)) = origin {
                    for (end, label, color) in ends {
                        if let Some((ex, ey)) = end {
                            paint_line_into(
                                ox,
                                oy,
                                ex,
                                ey,
                                0.0,
                                0.0,
                                color,
                                self.cw,
                                px_w,
                                px_h,
                                &mut self.dots,
                                &mut self.colors,
                                &mut self.depth_buf,
                                false,
                            );
                            labels.push((ex, ey, label, color));
                        }
                    }
                }
            }

            // Encode dots into screen cells
            let braille = self.braille;
            for cy in 0..ch {
                for cx in 0..cw {
                    let i = cy * cw + cx;
                    let p = self.dots[i];
                    if p != 0 {
                        screen.set(rx + cx, ry + cy, braille[p as usize], self.colors[i]);
                    }
                }
            }

            // Labels
            for (ex, ey, label, color) in labels {
                self.place_label(ex, ey, label, color, px_w, px_h, rx, ry, screen);
            }
        }

        #[allow(clippy::too_many_arguments)]
        fn place_label(
            &self,
            ex: f64,
            ey: f64,
            label: &str,
            color: u8,
            px_w: usize,
            px_h: usize,
            rx: usize,
            ry: usize,
            screen: &mut Screen,
        ) {
            if self.cw < 2 || self.ch < 2 {
                return;
            }
            let left = -(px_w as f64) / 2.0;
            let right = (px_w as f64) / 2.0;
            let top = (px_h as f64) / 2.0;
            let bottom = -(px_h as f64) / 2.0;
            let cell_w = (px_w as f64) / (self.cw - 1) as f64;
            let cell_h = (px_h as f64) / (self.ch - 1) as f64;
            let label_x = left + ((ex - left) / cell_w).round() * cell_w;
            let label_y = top - ((top - ey) / cell_h).round() * cell_h;
            if label_x >= left && label_x <= right && label_y <= top && label_y >= bottom {
                let x = ((label_x - left) * (self.cw - 1) as f64 / px_w as f64) as u16;
                let y = ((top - label_y) * (self.ch - 1) as f64 / px_h as f64) as u16;
                if let Some(ch) = label.chars().next() {
                    screen.set(rx + x as usize, ry + y as usize, ch, color);
                }
            }
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn paint_line_into(
        x1: f64,
        y1: f64,
        x2: f64,
        y2: f64,
        depth_a: f32,
        depth_b: f32,
        color: u8,
        cw: usize,
        px_w: usize,
        px_h: usize,
        dots: &mut [u8],
        colors: &mut [u8],
        depth_buf: &mut [f32],
        depth_test: bool,
    ) {
        let px_wf = px_w as f64;
        let px_hf = px_h as f64;
        let (left, right) = (-px_wf / 2.0, px_wf / 2.0);
        let (bottom, top) = (-px_hf / 2.0, px_hf / 2.0);
        let Some((cx1, cy1, cx2, cy2)) = clip_line(x1, y1, x2, y2, left, right, bottom, top) else {
            return;
        };
        let Some((dx1, dy1)) = get_point(cx1, cy1, px_w, px_h) else {
            return;
        };
        let Some((dx2, dy2)) = get_point(cx2, cy2, px_w, px_h) else {
            return;
        };

        // Compute depth at clipped endpoints via parametric interpolation
        let orig_len_sq = ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) as f32;
        let (cd1, cd2) = if orig_len_sq > 1e-12 {
            let t1 = (((cx1 - x1) * (x2 - x1) + (cy1 - y1) * (y2 - y1))
                / ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))) as f32;
            let t2 = (((cx2 - x1) * (x2 - x1) + (cy2 - y1) * (y2 - y1))
                / ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))) as f32;
            (
                depth_a + t1 * (depth_b - depth_a),
                depth_a + t2 * (depth_b - depth_a),
            )
        } else {
            (depth_a, depth_b)
        };

        let pix_len_sq = (dx2 as f32 - dx1 as f32).powi(2) + (dy2 as f32 - dy1 as f32).powi(2);
        bresenham(dx1, dy1, dx2, dy2, |x, y| {
            if !depth_test {
                paint_dot(x, y, color, cw, px_w, px_h, dots, colors);
                return;
            }
            // Interpolate depth at this pixel
            let depth = if pix_len_sq > 0.0 {
                let t = ((x as f32 - dx1 as f32) * (dx2 as f32 - dx1 as f32)
                    + (y as f32 - dy1 as f32) * (dy2 as f32 - dy1 as f32))
                    / pix_len_sq;
                cd1 + t * (cd2 - cd1)
            } else {
                cd1
            };
            let idx = y * px_w + x;
            if idx < depth_buf.len() && depth <= depth_buf[idx] {
                paint_dot(x, y, color, cw, px_w, px_h, dots, colors);
                depth_buf[idx] = depth;
            }
        });
    }

    /// Convert canvas coordinates to pixel coordinates for depth filling.
    fn canvas_to_pixel(x: f64, y: f64, px_w: usize, px_h: usize) -> (f32, f32) {
        let px_wf = px_w as f64;
        let px_hf = px_h as f64;
        let (left, _right) = (-px_wf / 2.0, px_wf / 2.0);
        let (_bottom, top) = (-px_hf / 2.0, px_hf / 2.0);
        let px = ((x - left) * (px_wf - 1.0) / px_wf) as f32;
        let py = ((top - y) * (px_hf - 1.0) / px_hf) as f32;
        (px, py)
    }

    /// Rasterize a triangle into the depth buffer only (no pixel output).
    #[allow(clippy::too_many_arguments)]
    fn fill_triangle_depth(
        x0: f32,
        y0: f32,
        z0: f32,
        x1: f32,
        y1: f32,
        z1: f32,
        x2: f32,
        y2: f32,
        z2: f32,
        px_w: usize,
        px_h: usize,
        depth_buf: &mut [f32],
    ) {
        // Sort vertices by y-coordinate
        let mut verts = [(x0, y0, z0), (x1, y1, z1), (x2, y2, z2)];
        verts.sort_by(|a, b| a.1.partial_cmp(&b.1).unwrap());
        let (ax, ay, az) = verts[0];
        let (bx, by, bz) = verts[1];
        let (cx, cy, cz) = verts[2];

        if (cy - ay).abs() < 0.001 {
            return; // degenerate triangle
        }

        let y_start = (ay.max(0.0)).ceil() as i32;
        let y_end = (cy.min(px_h as f32 - 1.0)).floor() as i32;

        for y in y_start..=y_end {
            let yf = y as f32;
            // Long edge AC
            let t_ac = (yf - ay) / (cy - ay);
            let x_ac = ax + t_ac * (cx - ax);
            let z_ac = az + t_ac * (cz - az);

            // Short edge: upper half (AB) or lower half (BC)
            let (x_short, z_short) = if yf <= by {
                let t = if (by - ay).abs() > 0.001 {
                    (yf - ay) / (by - ay)
                } else {
                    0.0
                };
                (ax + t * (bx - ax), az + t * (bz - az))
            } else {
                let t = if (cy - by).abs() > 0.001 {
                    (yf - by) / (cy - by)
                } else {
                    0.0
                };
                (bx + t * (cx - bx), bz + t * (cz - bz))
            };

            let (xl, xr, zl, zr) = if x_ac < x_short {
                (x_ac, x_short, z_ac, z_short)
            } else {
                (x_short, x_ac, z_short, z_ac)
            };

            let xi_start = (xl.max(0.0)).ceil() as i32;
            let xi_end = (xr.min(px_w as f32 - 1.0)).floor() as i32;
            let x_range = xr - xl;

            for x in xi_start..=xi_end {
                let t = if x_range > 0.001 {
                    (x as f32 - xl) / x_range
                } else {
                    0.0
                };
                let z = zl + t * (zr - zl);
                let idx = y as usize * px_w + x as usize;
                if idx < depth_buf.len() && z < depth_buf[idx] {
                    depth_buf[idx] = z;
                }
            }
        }
    }

    /// Fill the depth buffer with face depths for all boxes in the model.
    /// Each group of 8 consecutive vertices forms a box (box8 layout).
    fn fill_box_faces(
        proj: &[[f64; 2]],
        proj_ok: &[bool],
        proj_depth: &[f32],
        n_verts: usize,
        px_w: usize,
        px_h: usize,
        depth_buf: &mut [f32],
    ) {
        // Face vertex indices within each 8-vertex box
        const BOX_FACES: [[usize; 4]; 6] = [
            [0, 1, 2, 3], // bottom
            [4, 5, 6, 7], // top
            [0, 1, 5, 4], // front
            [1, 2, 6, 5], // right
            [2, 3, 7, 6], // back
            [3, 0, 4, 7], // left
        ];

        let mut offset = 0;
        while offset + 8 <= n_verts {
            let all_ok = (0..8).all(|i| proj_ok[offset + i]);
            if all_ok {
                for face in &BOX_FACES {
                    let idx: [usize; 4] = face.map(|i| offset + i);
                    // Get pixel coords and depths for each face vertex
                    let v: [(f32, f32, f32); 4] = std::array::from_fn(|i| {
                        let [cx, cy] = proj[idx[i]];
                        let (px, py) = canvas_to_pixel(cx, cy, px_w, px_h);
                        (px, py, proj_depth[idx[i]])
                    });
                    // Split quad into two triangles: (0,1,2) and (0,2,3)
                    fill_triangle_depth(
                        v[0].0, v[0].1, v[0].2, v[1].0, v[1].1, v[1].2, v[2].0, v[2].1, v[2].2,
                        px_w, px_h, depth_buf,
                    );
                    fill_triangle_depth(
                        v[0].0, v[0].1, v[0].2, v[2].0, v[2].1, v[2].2, v[3].0, v[3].1, v[3].2,
                        px_w, px_h, depth_buf,
                    );
                }
            }
            offset += 8;
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn clip_line(
        x1: f64,
        y1: f64,
        x2: f64,
        y2: f64,
        left: f64,
        right: f64,
        bottom: f64,
        top: f64,
    ) -> Option<(f64, f64, f64, f64)> {
        let mut p1 = (x1, y1);
        let mut p2 = (x2, y2);
        let mut r1 = region_code(p1, left, right, bottom, top);
        let mut r2 = region_code(p2, left, right, bottom, top);
        loop {
            if r1 & r2 != 0 {
                return None;
            }
            if r1 != 0 {
                p1 = intersect(p1, p2, r1, left, right, bottom, top);
                r1 = region_code(p1, left, right, bottom, top);
            } else if r2 != 0 {
                p2 = intersect(p2, p1, r2, left, right, bottom, top);
                r2 = region_code(p2, left, right, bottom, top);
            } else {
                return Some((p1.0, p1.1, p2.0, p2.1));
            }
        }
    }

    fn region_code(p: (f64, f64), left: f64, right: f64, bottom: f64, top: f64) -> u8 {
        let mut r = 0u8;
        if p.0 < left {
            r |= 1;
        } else if p.0 > right {
            r |= 2;
        }
        if p.1 < bottom {
            r |= 4;
        } else if p.1 > top {
            r |= 8;
        }
        r
    }

    fn intersect(
        p1: (f64, f64),
        p2: (f64, f64),
        region: u8,
        left: f64,
        right: f64,
        bottom: f64,
        top: f64,
    ) -> (f64, f64) {
        let dx = p2.0 - p1.0;
        let dy = p2.1 - p1.1;
        if region & 1 != 0 {
            let y = p1.1 + (left - p1.0) * dy / dx;
            return (left, y);
        }
        if region & 2 != 0 {
            let y = p1.1 + (right - p1.0) * dy / dx;
            return (right, y);
        }
        if region & 4 != 0 {
            let x = p1.0 + (bottom - p1.1) * dx / dy;
            return (x, bottom);
        }
        debug_assert!(region & 8 != 0);
        let x = p1.0 + (top - p1.1) * dx / dy;
        (x, top)
    }

    fn get_point(x: f64, y: f64, px_w: usize, px_h: usize) -> Option<(usize, usize)> {
        let px_wf = px_w as f64;
        let px_hf = px_h as f64;
        let (left, right) = (-px_wf / 2.0, px_wf / 2.0);
        let (bottom, top) = (-px_hf / 2.0, px_hf / 2.0);
        if x < left || x > right || y < bottom || y > top {
            return None;
        }
        let xd = ((x - left) * (px_wf - 1.0) / (right - left)).round() as usize;
        let yd = ((top - y) * (px_hf - 1.0) / (top - bottom)).round() as usize;
        Some((xd, yd))
    }

    #[allow(clippy::too_many_arguments)]
    #[inline]
    fn paint_dot(
        x: usize,
        y: usize,
        color: u8,
        cw: usize,
        px_w: usize,
        px_h: usize,
        dots: &mut [u8],
        colors: &mut [u8],
    ) {
        if x >= px_w || y >= px_h {
            return;
        }
        let cell = (y >> 2) * cw + (x >> 1);
        dots[cell] |= 1 << ((x & 1) + 2 * (y & 3));
        colors[cell] = color;
    }

    fn bresenham(x1: usize, y1: usize, x2: usize, y2: usize, mut f: impl FnMut(usize, usize)) {
        let dx = x2.abs_diff(x1);
        let dy = y2.abs_diff(y1);
        if dx == 0 {
            for y in y1.min(y2)..=y1.max(y2) {
                f(x1, y);
            }
        } else if dy == 0 {
            for x in x1.min(x2)..=x1.max(x2) {
                f(x, y1);
            }
        } else if dy < dx {
            if x1 > x2 {
                line_low(x2, y2, x1, y1, &mut f);
            } else {
                line_low(x1, y1, x2, y2, &mut f);
            }
        } else if y1 > y2 {
            line_high(x2, y2, x1, y1, &mut f);
        } else {
            line_high(x1, y1, x2, y2, &mut f);
        }
    }

    fn line_low(x1: usize, y1: usize, x2: usize, y2: usize, f: &mut impl FnMut(usize, usize)) {
        let dx = (x2 - x1) as isize;
        let dy = (y2 as isize - y1 as isize).abs();
        let mut d = 2 * dy - dx;
        let mut y = y1;
        for x in x1..=x2 {
            f(x, y);
            if d > 0 {
                y = if y1 > y2 {
                    y.saturating_sub(1)
                } else {
                    y.saturating_add(1)
                };
                d -= 2 * dx;
            }
            d += 2 * dy;
        }
    }

    fn line_high(x1: usize, y1: usize, x2: usize, y2: usize, f: &mut impl FnMut(usize, usize)) {
        let dx = (x2 as isize - x1 as isize).abs();
        let dy = (y2 - y1) as isize;
        let mut d = 2 * dx - dy;
        let mut x = x1;
        for y in y1..=y2 {
            f(x, y);
            if d > 0 {
                x = if x1 > x2 {
                    x.saturating_sub(1)
                } else {
                    x.saturating_add(1)
                };
                d -= 2 * dy;
            }
            d += 2 * dx;
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers};

    /// Build a plain key event
    fn key(code: KeyCode) -> Event {
        Event::Key(KeyEvent::new(code, KeyModifiers::NONE))
    }

    #[test]
    fn toggles_rain_pause_xray() {
        let mut app = App::new(4);

        // w: start/stop rain (toggle back and forth)
        assert!(!app.raining);
        assert!(!app.handle_input(key(KeyCode::Char('w'))));
        assert!(app.raining);
        app.handle_input(key(KeyCode::Char('w')));
        assert!(!app.raining);

        // p: pause/resume physics
        assert!(!app.paused);
        app.handle_input(key(KeyCode::Char('p')));
        assert!(app.paused);
        app.handle_input(key(KeyCode::Char('p')));
        assert!(!app.paused);

        // x: X-Ray see-through
        assert!(!app.xray);
        app.handle_input(key(KeyCode::Char('x')));
        assert!(app.xray);
        app.handle_input(key(KeyCode::Char('x')));
        assert!(!app.xray);
    }

    #[test]
    fn generate_rain_and_clear() {
        let mut app = App::new(4);
        // g: generate random terrain; heights scale with grid size (max = grid_size - 1 = 3)
        app.handle_input(key(KeyCode::Char('g')));
        assert!(
            app.terrain
                .heights
                .iter()
                .flatten()
                .all(|&h| h <= app.grid_size.saturating_sub(1) as u8)
        );

        // rain for a while to spawn particles
        app.handle_input(key(KeyCode::Char('w')));
        app.spawn_particles(2.0);
        assert!(app.particles.count() > 0);

        // c: clear particles and water levels
        app.handle_input(key(KeyCode::Char('c')));
        assert_eq!(app.particles.count(), 0);
        assert_eq!(app.trapped_water, 0);
        assert_eq!(app.physics.water.total_water(), 0.0);
    }

    #[test]
    fn terrain_heights_scale_with_grid_size() {
        // larger grid -> taller possible columns
        let mut big = App::new(16);
        big.handle_input(key(KeyCode::Char('g')));
        assert_eq!(big.terrain.max_height, 15);
        assert!(big.terrain.heights.iter().flatten().all(|&h| h <= 15));
        assert!(big.terrain.heights.iter().flatten().any(|&h| h > 7));

        // smaller grid -> shorter possible columns
        let mut small = App::new(4);
        small.handle_input(key(KeyCode::Char('g')));
        assert_eq!(small.terrain.max_height, 3);
        assert!(small.terrain.heights.iter().flatten().all(|&h| h <= 3));
    }

    #[test]
    fn quit_keys_exit() {
        assert!(App::new(4).handle_input(key(KeyCode::Char('q'))));
        assert!(App::new(4).handle_input(key(KeyCode::Esc)));
    }

    #[test]
    fn reset_view_preserves_center() {
        let mut app = App::new(8);
        // rotate the view a bit
        app.view.add_yaw(1.0);
        app.view.add_pitch(0.5);
        app.handle_input(key(KeyCode::Char('0')));
        // after reset the view returns to default and the rotation center stays at the container center (4,4,4)
        assert_eq!(app.view.center, [4.0, 4.0, 4.0]);
        assert_eq!(app.view.yaw, 0.0);
        assert_eq!(app.view.pitch, 0.0);
    }

    #[test]
    fn hud_layout_reflects_state() {
        let mut app = App::new(4);
        let (row0, row1) = hud_layout(&app);
        assert!(row0.contains("TRW"));
        assert!(row1.contains("paused: NO"));
        assert!(row1.contains("xray: NO"));
        app.toggle_pause();
        app.xray = true;
        let (_, row1) = hud_layout(&app);
        assert!(row1.contains("paused: YES"));
        assert!(row1.contains("xray: YES"));
    }
}