ftui-extras 0.3.1

Feature-gated extras for FrankenTUI (markdown, charts, clipboard, themes).
Documentation
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
//! Parsed Doom map with resolved cross-references.
//!
//! Converts raw WAD data into a usable map structure with f32 coordinates.

use super::wad::WadFile;
use super::wad_types::*;

/// A fully parsed Doom map ready for rendering and gameplay.
#[derive(Debug, Clone)]
pub struct DoomMap {
    pub name: String,
    pub vertices: Vec<Vertex>,
    pub linedefs: Vec<LineDef>,
    pub sidedefs: Vec<SideDef>,
    pub sectors: Vec<Sector>,
    pub segs: Vec<Seg>,
    pub subsectors: Vec<SubSector>,
    pub nodes: Vec<Node>,
    pub things: Vec<Thing>,
}

/// Map vertex in f32 coordinates.
#[derive(Debug, Clone, Copy)]
pub struct Vertex {
    pub x: f32,
    pub y: f32,
}

/// Resolved linedef with vertex positions and sidedef references.
#[derive(Debug, Clone)]
pub struct LineDef {
    pub v1: usize,
    pub v2: usize,
    pub flags: u16,
    pub special: u16,
    pub tag: u16,
    pub front_sidedef: Option<usize>,
    pub back_sidedef: Option<usize>,
}

impl LineDef {
    #[inline]
    pub fn is_two_sided(&self) -> bool {
        self.flags & ML_TWOSIDED != 0
    }

    #[inline]
    pub fn is_blocking(&self) -> bool {
        self.flags & ML_BLOCKING != 0
    }

    /// Get the front sector index (via front sidedef).
    pub fn front_sector(&self, sidedefs: &[SideDef]) -> Option<usize> {
        self.front_sidedef.map(|s| sidedefs[s].sector)
    }

    /// Get the back sector index (via back sidedef).
    pub fn back_sector(&self, sidedefs: &[SideDef]) -> Option<usize> {
        self.back_sidedef.map(|s| sidedefs[s].sector)
    }
}

/// Resolved sidedef.
#[derive(Debug, Clone)]
pub struct SideDef {
    pub x_offset: f32,
    pub y_offset: f32,
    pub upper_texture: String,
    pub lower_texture: String,
    pub middle_texture: String,
    pub sector: usize,
}

/// Resolved sector.
#[derive(Debug, Clone)]
pub struct Sector {
    pub floor_height: f32,
    pub ceiling_height: f32,
    pub floor_texture: String,
    pub ceiling_texture: String,
    pub light_level: u16,
    pub special: u16,
    pub tag: u16,
}

impl Sector {
    /// Check if this sector has a sky ceiling.
    pub fn is_sky_ceiling(&self) -> bool {
        self.ceiling_texture == "F_SKY1"
    }
}

/// Resolved seg (wall segment in BSP).
#[derive(Debug, Clone, Copy)]
pub struct Seg {
    pub v1: usize,
    pub v2: usize,
    pub angle: f32,
    pub linedef: usize,
    /// 0 = same direction as linedef, 1 = opposite.
    pub direction: u16,
    pub offset: f32,
}

/// Resolved subsector.
#[derive(Debug, Clone, Copy)]
pub struct SubSector {
    pub num_segs: u16,
    pub first_seg: usize,
}

/// Resolved BSP node.
#[derive(Debug, Clone, Copy)]
pub struct Node {
    /// Partition line start.
    pub x: f32,
    pub y: f32,
    /// Partition line direction.
    pub dx: f32,
    pub dy: f32,
    /// Bounding boxes [top, bottom, left, right].
    pub bbox_right: [f32; 4],
    pub bbox_left: [f32; 4],
    /// Right child: Some(node_index) or None for subsector leaf.
    pub right_child: NodeChild,
    /// Left child.
    pub left_child: NodeChild,
}

/// A BSP node child: either another node or a subsector leaf.
#[derive(Debug, Clone, Copy)]
pub enum NodeChild {
    Node(usize),
    SubSector(usize),
}

/// A thing (object/entity) placement.
#[derive(Debug, Clone, Copy)]
pub struct Thing {
    pub x: f32,
    pub y: f32,
    pub angle: f32,
    pub thing_type: u16,
    pub flags: u16,
}

impl DoomMap {
    /// Load a map from a WAD file by name (e.g., "E1M1" or "MAP01").
    pub fn load(wad: &WadFile, map_name: &str) -> Result<Self, super::wad::WadError> {
        let map_idx = wad
            .find_lump(map_name)
            .ok_or_else(|| super::wad::WadError::LumpNotFound(map_name.to_string()))?;

        // Map lumps follow the marker in a specific order
        let things_idx = wad
            .find_lump_after("THINGS", map_idx)
            .ok_or_else(|| super::wad::WadError::LumpNotFound("THINGS".into()))?;
        let linedefs_idx = wad
            .find_lump_after("LINEDEFS", map_idx)
            .ok_or_else(|| super::wad::WadError::LumpNotFound("LINEDEFS".into()))?;
        let sidedefs_idx = wad
            .find_lump_after("SIDEDEFS", map_idx)
            .ok_or_else(|| super::wad::WadError::LumpNotFound("SIDEDEFS".into()))?;
        let vertexes_idx = wad
            .find_lump_after("VERTEXES", map_idx)
            .ok_or_else(|| super::wad::WadError::LumpNotFound("VERTEXES".into()))?;
        let segs_idx = wad
            .find_lump_after("SEGS", map_idx)
            .ok_or_else(|| super::wad::WadError::LumpNotFound("SEGS".into()))?;
        let ssectors_idx = wad
            .find_lump_after("SSECTORS", map_idx)
            .ok_or_else(|| super::wad::WadError::LumpNotFound("SSECTORS".into()))?;
        let nodes_idx = wad
            .find_lump_after("NODES", map_idx)
            .ok_or_else(|| super::wad::WadError::LumpNotFound("NODES".into()))?;
        let sectors_idx = wad
            .find_lump_after("SECTORS", map_idx)
            .ok_or_else(|| super::wad::WadError::LumpNotFound("SECTORS".into()))?;

        // Parse raw data
        let raw_verts = wad.parse_vertices(vertexes_idx);
        let raw_linedefs = wad.parse_linedefs(linedefs_idx);
        let raw_sidedefs = wad.parse_sidedefs(sidedefs_idx);
        let raw_sectors = wad.parse_sectors(sectors_idx);
        let raw_segs = wad.parse_segs(segs_idx);
        let raw_ssectors = wad.parse_subsectors(ssectors_idx);
        let raw_nodes = wad.parse_nodes(nodes_idx);
        let raw_things = wad.parse_things(things_idx);

        // Convert to resolved types
        let vertices: Vec<Vertex> = raw_verts
            .iter()
            .map(|v| Vertex {
                x: v.x as f32,
                y: v.y as f32,
            })
            .collect();

        let sidedefs: Vec<SideDef> = raw_sidedefs
            .iter()
            .map(|s| SideDef {
                x_offset: s.x_offset as f32,
                y_offset: s.y_offset as f32,
                upper_texture: s.upper_name(),
                lower_texture: s.lower_name(),
                middle_texture: s.middle_name(),
                sector: s.sector as usize,
            })
            .collect();

        let linedefs: Vec<LineDef> = raw_linedefs
            .iter()
            .map(|l| LineDef {
                v1: l.v1 as usize,
                v2: l.v2 as usize,
                flags: l.flags,
                special: l.special,
                tag: l.tag,
                front_sidedef: if l.right_sidedef == 0xFFFF {
                    None
                } else {
                    Some(l.right_sidedef as usize)
                },
                back_sidedef: if l.left_sidedef == 0xFFFF {
                    None
                } else {
                    Some(l.left_sidedef as usize)
                },
            })
            .collect();

        let sectors: Vec<Sector> = raw_sectors
            .iter()
            .map(|s| Sector {
                floor_height: s.floor_height as f32,
                ceiling_height: s.ceiling_height as f32,
                floor_texture: s.floor_name(),
                ceiling_texture: s.ceiling_name(),
                light_level: s.light_level,
                special: s.special,
                tag: s.tag,
            })
            .collect();

        let segs: Vec<Seg> = raw_segs
            .iter()
            .map(|s| Seg {
                v1: s.v1 as usize,
                v2: s.v2 as usize,
                angle: (s.angle as f32) * std::f32::consts::PI / 32768.0,
                linedef: s.linedef as usize,
                direction: s.direction,
                offset: s.offset as f32,
            })
            .collect();

        let subsectors: Vec<SubSector> = raw_ssectors
            .iter()
            .map(|s| SubSector {
                num_segs: s.num_segs,
                first_seg: s.first_seg as usize,
            })
            .collect();

        let nodes: Vec<Node> = raw_nodes
            .iter()
            .map(|n| Node {
                x: n.x as f32,
                y: n.y as f32,
                dx: n.dx as f32,
                dy: n.dy as f32,
                bbox_right: [
                    n.bbox_right[0] as f32,
                    n.bbox_right[1] as f32,
                    n.bbox_right[2] as f32,
                    n.bbox_right[3] as f32,
                ],
                bbox_left: [
                    n.bbox_left[0] as f32,
                    n.bbox_left[1] as f32,
                    n.bbox_left[2] as f32,
                    n.bbox_left[3] as f32,
                ],
                right_child: parse_child(n.right_child),
                left_child: parse_child(n.left_child),
            })
            .collect();

        let things: Vec<Thing> = raw_things
            .iter()
            .map(|t| Thing {
                x: t.x as f32,
                y: t.y as f32,
                angle: (t.angle as f32) * std::f32::consts::PI / 180.0,
                thing_type: t.thing_type,
                flags: t.flags,
            })
            .collect();

        Ok(DoomMap {
            name: map_name.to_uppercase(),
            vertices,
            linedefs,
            sidedefs,
            sectors,
            segs,
            subsectors,
            nodes,
            things,
        })
    }

    /// Find the player 1 start position.
    pub fn player_start(&self) -> Option<(f32, f32, f32)> {
        self.things
            .iter()
            .find(|t| t.thing_type == THING_PLAYER1)
            .map(|t| (t.x, t.y, t.angle))
    }

    /// Find which subsector contains a point using BSP traversal.
    pub fn point_in_subsector(&self, x: f32, y: f32) -> usize {
        if self.nodes.is_empty() {
            return 0;
        }
        let mut node_idx = self.nodes.len() - 1; // Start at root
        loop {
            let node = &self.nodes[node_idx];
            let side = super::geometry::point_on_side(x, y, node.x, node.y, node.dx, node.dy);
            let child = if side {
                node.right_child
            } else {
                node.left_child
            };
            match child {
                NodeChild::SubSector(ss) => return ss,
                NodeChild::Node(n) => node_idx = n,
            }
        }
    }

    /// Get the sector that contains a point.
    pub fn point_sector(&self, x: f32, y: f32) -> Option<&Sector> {
        let ss_idx = self.point_in_subsector(x, y);
        let ss = &self.subsectors[ss_idx];
        if ss.num_segs == 0 {
            return None;
        }
        let seg = &self.segs[ss.first_seg];
        let linedef = &self.linedefs[seg.linedef];
        let sidedef_idx = if seg.direction == 0 {
            linedef.front_sidedef?
        } else {
            linedef.back_sidedef?
        };
        Some(&self.sectors[self.sidedefs[sidedef_idx].sector])
    }
}

fn parse_child(value: u16) -> NodeChild {
    if value & NF_SUBSECTOR != 0 {
        NodeChild::SubSector((value & !NF_SUBSECTOR) as usize)
    } else {
        NodeChild::Node(value as usize)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_child_node() {
        let NodeChild::Node(n) = parse_child(42) else {
            unreachable!("Expected Node variant");
        };
        assert_eq!(n, 42);
    }

    #[test]
    fn parse_child_subsector() {
        let NodeChild::SubSector(s) = parse_child(0x8005) else {
            unreachable!("Expected SubSector variant");
        };
        assert_eq!(s, 5);
    }

    #[test]
    fn parse_child_node_zero() {
        let NodeChild::Node(n) = parse_child(0) else {
            unreachable!("Expected Node variant");
        };
        assert_eq!(n, 0);
    }

    #[test]
    fn parse_child_subsector_zero() {
        let NodeChild::SubSector(s) = parse_child(NF_SUBSECTOR) else {
            unreachable!("Expected SubSector variant");
        };
        assert_eq!(s, 0);
    }

    #[test]
    fn linedef_is_two_sided() {
        let ld = LineDef {
            v1: 0,
            v2: 1,
            flags: ML_TWOSIDED,
            special: 0,
            tag: 0,
            front_sidedef: None,
            back_sidedef: None,
        };
        assert!(ld.is_two_sided());
    }

    #[test]
    fn linedef_not_two_sided() {
        let ld = LineDef {
            v1: 0,
            v2: 1,
            flags: 0,
            special: 0,
            tag: 0,
            front_sidedef: None,
            back_sidedef: None,
        };
        assert!(!ld.is_two_sided());
    }

    #[test]
    fn linedef_is_blocking() {
        let ld = LineDef {
            v1: 0,
            v2: 1,
            flags: ML_BLOCKING,
            special: 0,
            tag: 0,
            front_sidedef: None,
            back_sidedef: None,
        };
        assert!(ld.is_blocking());
    }

    #[test]
    fn linedef_not_blocking() {
        let ld = LineDef {
            v1: 0,
            v2: 1,
            flags: ML_TWOSIDED,
            special: 0,
            tag: 0,
            front_sidedef: None,
            back_sidedef: None,
        };
        assert!(!ld.is_blocking());
    }

    #[test]
    fn linedef_front_sector_some() {
        let sidedefs = vec![SideDef {
            x_offset: 0.0,
            y_offset: 0.0,
            upper_texture: String::new(),
            lower_texture: String::new(),
            middle_texture: String::new(),
            sector: 3,
        }];
        let ld = LineDef {
            v1: 0,
            v2: 1,
            flags: 0,
            special: 0,
            tag: 0,
            front_sidedef: Some(0),
            back_sidedef: None,
        };
        assert_eq!(ld.front_sector(&sidedefs), Some(3));
    }

    #[test]
    fn linedef_front_sector_none() {
        let sidedefs: Vec<SideDef> = vec![];
        let ld = LineDef {
            v1: 0,
            v2: 1,
            flags: 0,
            special: 0,
            tag: 0,
            front_sidedef: None,
            back_sidedef: None,
        };
        assert_eq!(ld.front_sector(&sidedefs), None);
    }

    #[test]
    fn linedef_back_sector_some() {
        let sidedefs = vec![SideDef {
            x_offset: 0.0,
            y_offset: 0.0,
            upper_texture: String::new(),
            lower_texture: String::new(),
            middle_texture: String::new(),
            sector: 7,
        }];
        let ld = LineDef {
            v1: 0,
            v2: 1,
            flags: 0,
            special: 0,
            tag: 0,
            front_sidedef: None,
            back_sidedef: Some(0),
        };
        assert_eq!(ld.back_sector(&sidedefs), Some(7));
    }

    #[test]
    fn linedef_combined_flags() {
        let ld = LineDef {
            v1: 0,
            v2: 1,
            flags: ML_BLOCKING | ML_TWOSIDED,
            special: 0,
            tag: 0,
            front_sidedef: None,
            back_sidedef: None,
        };
        assert!(ld.is_blocking());
        assert!(ld.is_two_sided());
    }

    #[test]
    fn sector_is_sky_ceiling() {
        let s = Sector {
            floor_height: 0.0,
            ceiling_height: 128.0,
            floor_texture: String::new(),
            ceiling_texture: "F_SKY1".into(),
            light_level: 200,
            special: 0,
            tag: 0,
        };
        assert!(s.is_sky_ceiling());
    }

    #[test]
    fn sector_not_sky_ceiling() {
        let s = Sector {
            floor_height: 0.0,
            ceiling_height: 128.0,
            floor_texture: String::new(),
            ceiling_texture: "FLAT1".into(),
            light_level: 200,
            special: 0,
            tag: 0,
        };
        assert!(!s.is_sky_ceiling());
    }

    #[test]
    fn point_in_subsector_empty_nodes() {
        let map = DoomMap {
            name: "TEST".into(),
            vertices: vec![],
            linedefs: vec![],
            sidedefs: vec![],
            sectors: vec![],
            segs: vec![],
            subsectors: vec![],
            nodes: vec![],
            things: vec![],
        };
        assert_eq!(map.point_in_subsector(0.0, 0.0), 0);
    }

    #[test]
    fn vertex_coords() {
        let v = Vertex { x: 10.5, y: -20.3 };
        assert!((v.x - 10.5).abs() < f32::EPSILON);
        assert!((v.y + 20.3).abs() < f32::EPSILON);
    }

    #[test]
    fn thing_fields() {
        let t = Thing {
            x: 100.0,
            y: 200.0,
            angle: 1.5,
            thing_type: THING_PLAYER1,
            flags: 0,
        };
        assert_eq!(t.thing_type, THING_PLAYER1);
        assert!((t.angle - 1.5).abs() < f32::EPSILON);
    }

    #[test]
    fn seg_fields() {
        let s = Seg {
            v1: 0,
            v2: 1,
            angle: std::f32::consts::PI,
            linedef: 5,
            direction: 0,
            offset: 10.0,
        };
        assert_eq!(s.linedef, 5);
        assert_eq!(s.direction, 0);
    }

    #[test]
    fn subsector_fields() {
        let ss = SubSector {
            num_segs: 4,
            first_seg: 10,
        };
        assert_eq!(ss.num_segs, 4);
        assert_eq!(ss.first_seg, 10);
    }

    #[test]
    fn player_start_found() {
        let map = DoomMap {
            name: "TEST".into(),
            vertices: vec![],
            linedefs: vec![],
            sidedefs: vec![],
            sectors: vec![],
            segs: vec![],
            subsectors: vec![],
            nodes: vec![],
            things: vec![
                Thing {
                    x: 50.0,
                    y: 60.0,
                    angle: 0.0,
                    thing_type: 0,
                    flags: 0,
                },
                Thing {
                    x: 100.0,
                    y: 200.0,
                    angle: 1.5,
                    thing_type: THING_PLAYER1,
                    flags: 0x07,
                },
            ],
        };
        let start = map.player_start();
        assert!(start.is_some());
        let (x, y, a) = start.unwrap();
        assert!((x - 100.0).abs() < f32::EPSILON);
        assert!((y - 200.0).abs() < f32::EPSILON);
        assert!((a - 1.5).abs() < f32::EPSILON);
    }

    #[test]
    fn player_start_not_found() {
        let map = DoomMap {
            name: "TEST".into(),
            vertices: vec![],
            linedefs: vec![],
            sidedefs: vec![],
            sectors: vec![],
            segs: vec![],
            subsectors: vec![],
            nodes: vec![],
            things: vec![Thing {
                x: 50.0,
                y: 60.0,
                angle: 0.0,
                thing_type: 0,
                flags: 0,
            }],
        };
        assert!(map.player_start().is_none());
    }

    // --- Additional edge case tests (bd-152kz) ---

    #[test]
    fn player_start_empty_things() {
        let map = DoomMap {
            name: "TEST".into(),
            vertices: vec![],
            linedefs: vec![],
            sidedefs: vec![],
            sectors: vec![],
            segs: vec![],
            subsectors: vec![],
            nodes: vec![],
            things: vec![],
        };
        assert!(map.player_start().is_none());
    }

    #[test]
    fn player_start_picks_first() {
        let map = DoomMap {
            name: "TEST".into(),
            vertices: vec![],
            linedefs: vec![],
            sidedefs: vec![],
            sectors: vec![],
            segs: vec![],
            subsectors: vec![],
            nodes: vec![],
            things: vec![
                Thing {
                    x: 10.0,
                    y: 20.0,
                    angle: 0.0,
                    thing_type: THING_PLAYER1,
                    flags: 0,
                },
                Thing {
                    x: 99.0,
                    y: 99.0,
                    angle: 3.0,
                    thing_type: THING_PLAYER1,
                    flags: 0,
                },
            ],
        };
        let (x, y, _) = map.player_start().unwrap();
        assert!(
            (x - 10.0).abs() < f32::EPSILON,
            "should pick first player start"
        );
        assert!((y - 20.0).abs() < f32::EPSILON);
    }

    #[test]
    fn linedef_back_sector_none() {
        let sidedefs: Vec<SideDef> = vec![];
        let ld = LineDef {
            v1: 0,
            v2: 1,
            flags: 0,
            special: 0,
            tag: 0,
            front_sidedef: None,
            back_sidedef: None,
        };
        assert_eq!(ld.back_sector(&sidedefs), None);
    }

    #[test]
    fn linedef_both_sidedefs() {
        let sidedefs = vec![
            SideDef {
                x_offset: 0.0,
                y_offset: 0.0,
                upper_texture: String::new(),
                lower_texture: String::new(),
                middle_texture: String::new(),
                sector: 2,
            },
            SideDef {
                x_offset: 0.0,
                y_offset: 0.0,
                upper_texture: String::new(),
                lower_texture: String::new(),
                middle_texture: String::new(),
                sector: 5,
            },
        ];
        let ld = LineDef {
            v1: 0,
            v2: 1,
            flags: ML_TWOSIDED,
            special: 0,
            tag: 0,
            front_sidedef: Some(0),
            back_sidedef: Some(1),
        };
        assert_eq!(ld.front_sector(&sidedefs), Some(2));
        assert_eq!(ld.back_sector(&sidedefs), Some(5));
    }

    #[test]
    fn parse_child_max_subsector() {
        // NF_SUBSECTOR | 0x7FFF = 0xFFFF
        let NodeChild::SubSector(s) = parse_child(0xFFFF) else {
            unreachable!("Expected SubSector variant");
        };
        assert_eq!(s, 0x7FFF);
    }

    #[test]
    fn parse_child_max_node() {
        // Just below NF_SUBSECTOR threshold
        let NodeChild::Node(n) = parse_child(0x7FFF) else {
            unreachable!("Expected Node variant");
        };
        assert_eq!(n, 0x7FFF);
    }

    #[test]
    fn sector_is_sky_case_sensitive() {
        let s = Sector {
            floor_height: 0.0,
            ceiling_height: 128.0,
            floor_texture: String::new(),
            ceiling_texture: "f_sky1".into(),
            light_level: 200,
            special: 0,
            tag: 0,
        };
        assert!(!s.is_sky_ceiling(), "should be case-sensitive");
    }

    #[test]
    fn node_child_debug() {
        let node = NodeChild::Node(42);
        let ss = NodeChild::SubSector(7);
        let nd = format!("{node:?}");
        let sd = format!("{ss:?}");
        assert!(nd.contains("Node"));
        assert!(nd.contains("42"));
        assert!(sd.contains("SubSector"));
        assert!(sd.contains("7"));
    }

    #[test]
    fn node_fields() {
        let n = Node {
            x: 10.0,
            y: 20.0,
            dx: 1.0,
            dy: 0.0,
            bbox_right: [30.0, 10.0, 10.0, 30.0],
            bbox_left: [30.0, 10.0, -10.0, 10.0],
            right_child: NodeChild::SubSector(0),
            left_child: NodeChild::Node(1),
        };
        assert!((n.x - 10.0).abs() < f32::EPSILON);
        assert!((n.dy - 0.0).abs() < f32::EPSILON);
        assert!(matches!(n.right_child, NodeChild::SubSector(0)));
        assert!(matches!(n.left_child, NodeChild::Node(1)));
    }

    #[test]
    fn sidedef_fields() {
        let s = SideDef {
            x_offset: 16.0,
            y_offset: -8.0,
            upper_texture: "UPPER".into(),
            lower_texture: "LOWER".into(),
            middle_texture: "MID".into(),
            sector: 3,
        };
        assert!((s.x_offset - 16.0).abs() < f32::EPSILON);
        assert!((s.y_offset + 8.0).abs() < f32::EPSILON);
        assert_eq!(s.upper_texture, "UPPER");
        assert_eq!(s.lower_texture, "LOWER");
        assert_eq!(s.middle_texture, "MID");
        assert_eq!(s.sector, 3);
    }

    #[test]
    fn doom_map_clone() {
        let map = DoomMap {
            name: "E1M1".into(),
            vertices: vec![Vertex { x: 1.0, y: 2.0 }],
            linedefs: vec![],
            sidedefs: vec![],
            sectors: vec![],
            segs: vec![],
            subsectors: vec![],
            nodes: vec![],
            things: vec![],
        };
        let cloned = map.clone();
        assert_eq!(cloned.name, "E1M1");
        assert_eq!(cloned.vertices.len(), 1);
    }

    #[test]
    fn doom_map_debug() {
        let map = DoomMap {
            name: "MAP01".into(),
            vertices: vec![],
            linedefs: vec![],
            sidedefs: vec![],
            sectors: vec![],
            segs: vec![],
            subsectors: vec![],
            nodes: vec![],
            things: vec![],
        };
        let dbg = format!("{map:?}");
        assert!(dbg.contains("DoomMap"));
        assert!(dbg.contains("MAP01"));
    }

    #[test]
    fn doom_map_name_uppercase() {
        // The load function uppercases the name - verify the field stores it
        let map = DoomMap {
            name: "e1m1".to_uppercase(),
            vertices: vec![],
            linedefs: vec![],
            sidedefs: vec![],
            sectors: vec![],
            segs: vec![],
            subsectors: vec![],
            nodes: vec![],
            things: vec![],
        };
        assert_eq!(map.name, "E1M1");
    }

    #[test]
    fn vertex_copy_semantics() {
        let v1 = Vertex { x: 1.0, y: 2.0 };
        let v2 = v1; // Copy
        assert!((v1.x - v2.x).abs() < f32::EPSILON);
        assert!((v1.y - v2.y).abs() < f32::EPSILON);
    }

    #[test]
    fn seg_copy_semantics() {
        let s1 = Seg {
            v1: 0,
            v2: 1,
            angle: 0.5,
            linedef: 3,
            direction: 1,
            offset: 2.0,
        };
        let s2 = s1; // Copy
        assert_eq!(s1.v1, s2.v1);
        assert_eq!(s1.linedef, s2.linedef);
    }

    #[test]
    fn thing_copy_semantics() {
        let t1 = Thing {
            x: 1.0,
            y: 2.0,
            angle: 3.0,
            thing_type: 42,
            flags: 7,
        };
        let t2 = t1; // Copy
        assert_eq!(t1.thing_type, t2.thing_type);
        assert_eq!(t1.flags, t2.flags);
    }

    #[test]
    fn sector_clone_and_fields() {
        let s = Sector {
            floor_height: -16.0,
            ceiling_height: 256.0,
            floor_texture: "FLOOR4_8".into(),
            ceiling_texture: "CEIL3_5".into(),
            light_level: 160,
            special: 9,
            tag: 42,
        };
        let cloned = s.clone();
        assert!((cloned.floor_height + 16.0).abs() < f32::EPSILON);
        assert!((cloned.ceiling_height - 256.0).abs() < f32::EPSILON);
        assert_eq!(cloned.floor_texture, "FLOOR4_8");
        assert_eq!(cloned.light_level, 160);
        assert_eq!(cloned.special, 9);
        assert_eq!(cloned.tag, 42);
    }
}