nwnrs-set 0.0.8

Typed Neverwinter Nights tileset SET parser
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
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
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

use std::{
    collections::BTreeMap,
    fmt,
    fs::File,
    io::{self, Read, Write},
    path::Path,
};

use nwnrs_resman::prelude::*;
use nwnrs_resref::prelude::ResolvedResRef;
use nwnrs_restype::prelude::*;
use tracing::instrument;

/// NWN resource type id for `set`.
pub const SET_RES_TYPE: ResType = ResType(2013);

/// Errors returned while reading or parsing `SET` payloads.
///
/// # Examples
///
/// ```rust,no_run
/// let _ = std::mem::size_of::<nwnrs_set::SetError>();
/// ```
#[derive(Debug)]
pub enum SetError {
    /// An underlying IO operation failed.
    Io(io::Error),
    /// Resource-manager access failed.
    ResMan(ResManError),
    /// The payload was otherwise invalid or unsupported.
    Message(String),
}

impl SetError {
    /// Creates a free-form `SET` error message.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// let _ = nwnrs_set::SetError::msg;
    /// ```
    pub fn msg(message: impl Into<String>) -> Self {
        Self::Message(message.into())
    }
}

impl fmt::Display for SetError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Io(error) => error.fmt(f),
            Self::ResMan(error) => error.fmt(f),
            Self::Message(message) => f.write_str(message),
        }
    }
}

impl std::error::Error for SetError {}

impl From<io::Error> for SetError {
    fn from(value: io::Error) -> Self {
        Self::Io(value)
    }
}

impl From<ResManError> for SetError {
    fn from(value: ResManError) -> Self {
        Self::ResMan(value)
    }
}

/// Result type for `SET` operations.
pub type SetResult<T> = Result<T, SetError>;

/// Parsed tileset payload.
///
/// The representation preserves the authored section structure explicitly:
/// top-level metadata, terrain and crosser catalogs, primary rules, tiles,
/// tile-door metadata, and groups remain distinct keyed collections rather than
/// being flattened into one generic map.
///
/// # Examples
///
/// ```rust,no_run
/// let set_file = nwnrs_set::SetFile::default();
/// assert!(set_file.tiles.is_empty());
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SetFile {
    /// Top-level `[GENERAL]` metadata.
    pub general:       SetGeneral,
    /// Optional `[GRASS]` block.
    pub grass:         Option<SetGrass>,
    /// `[TERRAINN]` entries keyed by terrain id.
    pub terrains:      BTreeMap<u32, SetNamedType>,
    /// `[CROSSERN]` entries keyed by crosser id.
    pub crossers:      BTreeMap<u32, SetNamedType>,
    /// `[PRIMARY RULEN]` entries keyed by rule id.
    pub primary_rules: BTreeMap<u32, SetPrimaryRule>,
    /// `[TILEN]` entries keyed by tile id.
    pub tiles:         BTreeMap<u32, SetTile>,
    /// `[TILENMDOORK]` entries keyed by `(tile_id, door_id)`.
    pub tile_doors:    BTreeMap<(u32, u32), SetTileDoor>,
    /// `[GROUPN]` entries keyed by group id.
    pub groups:        BTreeMap<u32, SetGroup>,
}

impl SetFile {
    /// Reads a typed `SET` file from disk.
    ///
    /// # Errors
    ///
    /// Returns [`SetError`] if the file cannot be opened or parsed.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// let _ = nwnrs_set::SetFile::from_file;
    /// ```
    pub fn from_file(path: impl AsRef<Path>) -> SetResult<Self> {
        let mut file = File::open(path.as_ref())?;
        read_set(&mut file)
    }

    /// Reads a typed `SET` file from a [`Res`].
    ///
    /// # Errors
    ///
    /// Returns [`SetError`] if the resource is not a SET type or the bytes
    /// cannot be parsed.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// let _ = nwnrs_set::SetFile::from_res;
    /// ```
    pub fn from_res(res: &Res, cache_policy: CachePolicy) -> SetResult<Self> {
        if res.resref().res_type() != SET_RES_TYPE {
            return Err(SetError::msg(format!(
                "expected set resource, got {}",
                res.resref()
            )));
        }

        let bytes = res.read_all(cache_policy)?;
        let text = String::from_utf8(bytes)
            .map_err(|error| SetError::msg(format!("SET payload is not valid UTF-8: {error}")))?;
        parse_set(&text)
    }

    /// Reads a typed `SET` file from a [`ResMan`] by tileset name.
    ///
    /// # Examples
    ///
    /// ```rust,no_run
    /// let _ = nwnrs_set::SetFile::from_resman;
    /// ```
    pub fn from_resman(
        resman: &mut ResMan,
        set_name: &str,
        cache_policy: CachePolicy,
    ) -> SetResult<Self> {
        let resolved = ResolvedResRef::from_filename(&format!("{set_name}.set"))
            .map_err(|error| SetError::msg(format!("set resref: {error}")))?;
        let res = resman
            .get_resolved(&resolved)
            .ok_or_else(|| SetError::msg(format!("tileset not found in ResMan: {resolved}")))?;
        Self::from_res(&res, cache_policy)
    }
}

/// Parsed `[GENERAL]` section.
///
/// # Examples
///
/// ```rust,no_run
/// let general = nwnrs_set::SetGeneral::default();
/// assert!(general.name.is_none());
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SetGeneral {
    /// Internal tileset name.
    pub name:                  Option<String>,
    /// Declared resource type, usually `SET`.
    pub file_type:             Option<String>,
    /// Declared version string.
    pub version:               Option<String>,
    /// Whether the tileset is interior.
    pub interior:              Option<bool>,
    /// Whether height transitions are enabled.
    pub has_height_transition: Option<bool>,
    /// Environment map name.
    pub env_map:               Option<String>,
    /// Transition type id.
    pub transition:            Option<i32>,
    /// Selector height hint.
    pub selector_height:       Option<i32>,
    /// Dialog.tlk string reference for the localized display name.
    pub display_name:          Option<i32>,
    /// Fallback unlocalized display name.
    pub unlocalized_name:      Option<String>,
    /// Default border terrain tag.
    pub border:                Option<String>,
    /// Default terrain tag.
    pub default_terrain:       Option<String>,
    /// Default floor terrain tag.
    pub floor:                 Option<String>,
}

/// Parsed `[GRASS]` section.
///
/// # Examples
///
/// ```rust,no_run
/// let grass = nwnrs_set::SetGrass::default();
/// assert!(grass.texture_name.is_none());
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SetGrass {
    /// Whether grass rendering is enabled.
    pub grass:        Option<bool>,
    /// Grass texture resource name.
    pub texture_name: Option<String>,
    /// Grass density value.
    pub density:      Option<f32>,
    /// Grass height value.
    pub height:       Option<f32>,
    /// Ambient grass color.
    pub ambient:      Option<[f32; 3]>,
    /// Diffuse grass color.
    pub diffuse:      Option<[f32; 3]>,
}

/// Named tileset catalog entry such as `[TERRAIN0]` or `[CROSSER0]`.
///
/// # Examples
///
/// ```rust,no_run
/// let named = nwnrs_set::SetNamedType::default();
/// assert_eq!(named.id, 0);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SetNamedType {
    /// Entry id from the section suffix.
    pub id:      u32,
    /// Display or symbolic name.
    pub name:    Option<String>,
    /// Optional dialog.tlk string reference.
    pub str_ref: Option<i32>,
}

/// One terrain corner annotation on a tile.
///
/// # Examples
///
/// ```rust,no_run
/// let corner = nwnrs_set::SetTileCorner::default();
/// assert!(corner.terrain.is_none());
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SetTileCorner {
    /// Terrain tag for this corner.
    pub terrain: Option<String>,
    /// Height step at this corner.
    pub height:  Option<i32>,
}

/// One set of edge crosser tags on a tile.
///
/// # Examples
///
/// ```rust,no_run
/// let edges = nwnrs_set::SetTileEdges::default();
/// assert!(edges.top.is_none());
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SetTileEdges {
    /// Crosser tag on the top edge.
    pub top:    Option<String>,
    /// Crosser tag on the right edge.
    pub right:  Option<String>,
    /// Crosser tag on the bottom edge.
    pub bottom: Option<String>,
    /// Crosser tag on the left edge.
    pub left:   Option<String>,
}

/// Parsed `[TILEN]` section.
///
/// # Examples
///
/// ```rust,no_run
/// let tile = nwnrs_set::SetTile::default();
/// assert_eq!(tile.id, 0);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SetTile {
    /// Tile id from the section suffix.
    pub id: u32,
    /// MDL resource name.
    pub model: Option<String>,
    /// Walkmesh identifier.
    pub walkmesh: Option<String>,
    /// Top-left terrain annotation.
    pub top_left: SetTileCorner,
    /// Top-right terrain annotation.
    pub top_right: SetTileCorner,
    /// Bottom-left terrain annotation.
    pub bottom_left: SetTileCorner,
    /// Bottom-right terrain annotation.
    pub bottom_right: SetTileCorner,
    /// Edge crosser tags.
    pub edge_crossers: SetTileEdges,
    /// First main-light flag.
    pub main_light_1: Option<bool>,
    /// Second main-light flag.
    pub main_light_2: Option<bool>,
    /// First source-light flag.
    pub source_light_1: Option<bool>,
    /// Second source-light flag.
    pub source_light_2: Option<bool>,
    /// First animation-loop flag.
    pub anim_loop_1: Option<bool>,
    /// Second animation-loop flag.
    pub anim_loop_2: Option<bool>,
    /// Third animation-loop flag.
    pub anim_loop_3: Option<bool>,
    /// Door count declared on the tile.
    pub doors: Option<u32>,
    /// Sound count declared on the tile.
    pub sounds: Option<u32>,
    /// Path node marker.
    pub path_node: Option<String>,
    /// Path node orientation.
    pub orientation: Option<i32>,
    /// Visibility node marker.
    pub visibility_node: Option<String>,
    /// Visibility node orientation.
    pub visibility_orientation: Option<i32>,
    /// Optional door visibility node marker.
    pub door_visibility_node: Option<String>,
    /// Optional door visibility node orientation.
    pub door_visibility_orientation: Option<i32>,
    /// 2D selector image name.
    pub image_map_2d: Option<String>,
}

/// Parsed `[TILENDOORK]` section.
///
/// # Examples
///
/// ```rust,no_run
/// let door = nwnrs_set::SetTileDoor::default();
/// assert_eq!(door.tile_id, 0);
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
pub struct SetTileDoor {
    /// Tile id from the section prefix.
    pub tile_id:     u32,
    /// Door id from the section suffix.
    pub door_id:     u32,
    /// Door type identifier.
    pub door_type:   Option<i32>,
    /// Door marker X coordinate.
    pub x:           Option<f32>,
    /// Door marker Y coordinate.
    pub y:           Option<f32>,
    /// Door marker Z coordinate.
    pub z:           Option<f32>,
    /// Door marker orientation.
    pub orientation: Option<i32>,
}

/// Parsed `[GROUPN]` section.
///
/// # Examples
///
/// ```rust,no_run
/// let group = nwnrs_set::SetGroup::default();
/// assert!(group.tiles.is_empty());
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SetGroup {
    /// Group id from the section suffix.
    pub id:      u32,
    /// Group display name.
    pub name:    Option<String>,
    /// Optional dialog.tlk string reference.
    pub str_ref: Option<i32>,
    /// Group row count.
    pub rows:    Option<u32>,
    /// Group column count.
    pub columns: Option<u32>,
    /// Group tile layout keyed by zero-based cell index.
    pub tiles:   BTreeMap<u32, Option<u32>>,
}

/// Parsed `[PRIMARY RULEN]` section.
///
/// # Examples
///
/// ```rust,no_run
/// let rule = nwnrs_set::SetPrimaryRule::default();
/// assert_eq!(rule.id, 0);
/// ```
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct SetPrimaryRule {
    /// Rule id from the section suffix.
    pub id:              u32,
    /// Terrain tag for the placed tile.
    pub placed:          Option<String>,
    /// Height for the placed terrain.
    pub placed_height:   Option<i32>,
    /// Terrain tag for the adjacent tile.
    pub adjacent:        Option<String>,
    /// Height for the adjacent terrain.
    pub adjacent_height: Option<i32>,
    /// Terrain tag after applying the rule.
    pub changed:         Option<String>,
    /// Height after applying the rule.
    pub changed_height:  Option<i32>,
}

/// Reads a typed `SET` file from `reader`.
///
/// # Errors
///
/// Returns [`SetError`] if the data cannot be read or does not conform to the
/// SET format.
///
/// # Examples
///
/// ```rust,no_run
/// let _ = nwnrs_set::read_set;
/// ```
#[instrument(level = "debug", skip_all, err)]
pub fn read_set<R: Read>(reader: &mut R) -> SetResult<SetFile> {
    let mut text = String::new();
    reader.read_to_string(&mut text)?;
    parse_set(&text)
}

/// Parses a typed `SET` file from text.
///
/// # Errors
///
/// Returns [`SetError`] if the text contains no tile definitions.
///
/// # Examples
///
/// ```rust,no_run
/// let _ = nwnrs_set::parse_set;
/// ```
pub fn parse_set(text: &str) -> SetResult<SetFile> {
    let mut builder = SetFile::default();
    let mut current_section = String::new();
    let mut current_entries = BTreeMap::new();

    for raw_line in text.lines() {
        let line = raw_line.trim();
        if line.is_empty() || line.starts_with(';') || line.starts_with("//") {
            continue;
        }

        if line.starts_with('[') && line.ends_with(']') {
            if !current_section.is_empty() {
                apply_section(&mut builder, &current_section, &current_entries);
                current_entries.clear();
            }
            current_section = line[1..line.len() - 1].trim().to_string();
            continue;
        }

        let Some((key, value)) = line.split_once('=') else {
            continue;
        };
        current_entries.insert(key.trim().to_ascii_lowercase(), value.trim().to_string());
    }

    if !current_section.is_empty() {
        apply_section(&mut builder, &current_section, &current_entries);
    }

    if builder.tiles.is_empty() {
        return Err(SetError::msg(
            "tileset file contained no tile definitions".to_string(),
        ));
    }

    Ok(builder)
}

/// Builds deterministic `SET` text from a typed [`SetFile`].
///
/// The serializer emits the modeled section structure explicitly: general and
/// grass blocks first, followed by synthesized catalog count sections and then
/// the indexed terrain, crosser, rule, tile, tile-door, and group sections in
/// ascending key order.
///
/// # Errors
///
/// Returns [`SetError`] if the file contains no tile definitions.
///
/// # Examples
///
/// ```rust,no_run
/// let _ = nwnrs_set::build_set_text;
/// ```
pub fn build_set_text(set_file: &SetFile) -> SetResult<String> {
    if set_file.tiles.is_empty() {
        return Err(SetError::msg(
            "cannot build SET payload without at least one tile definition",
        ));
    }

    let mut text = String::new();
    write_general(&mut text, &set_file.general);
    if let Some(grass) = &set_file.grass {
        push_blank_line(&mut text);
        write_grass(&mut text, grass);
    }

    push_blank_line(&mut text);
    write_count_section(&mut text, "TERRAIN TYPES", set_file.terrains.len());
    for (id, terrain) in &set_file.terrains {
        push_blank_line(&mut text);
        write_named_type_section(&mut text, &format!("TERRAIN{id}"), terrain);
    }

    push_blank_line(&mut text);
    write_count_section(&mut text, "CROSSER TYPES", set_file.crossers.len());
    for (id, crosser) in &set_file.crossers {
        push_blank_line(&mut text);
        write_named_type_section(&mut text, &format!("CROSSER{id}"), crosser);
    }

    push_blank_line(&mut text);
    write_count_section(&mut text, "PRIMARY RULES", set_file.primary_rules.len());
    for (id, rule) in &set_file.primary_rules {
        push_blank_line(&mut text);
        write_primary_rule_section(&mut text, &format!("PRIMARY RULE{id}"), rule);
    }

    push_blank_line(&mut text);
    write_count_section(&mut text, "TILES", set_file.tiles.len());
    for (id, tile) in &set_file.tiles {
        push_blank_line(&mut text);
        write_tile_section(&mut text, &format!("TILE{id}"), tile);
    }

    for ((tile_id, door_id), door) in &set_file.tile_doors {
        push_blank_line(&mut text);
        write_tile_door_section(&mut text, &format!("TILE{tile_id}DOOR{door_id}"), door);
    }

    push_blank_line(&mut text);
    write_count_section(&mut text, "GROUPS", set_file.groups.len());
    for (id, group) in &set_file.groups {
        push_blank_line(&mut text);
        write_group_section(&mut text, &format!("GROUP{id}"), group);
    }

    Ok(text)
}

/// Writes deterministic `SET` text to `writer`.
///
/// # Errors
///
/// Returns [`SetError`] if the file contains no tile definitions or the write
/// fails.
///
/// # Examples
///
/// ```rust,no_run
/// let _ = nwnrs_set::write_set;
/// ```
pub fn write_set<W: Write>(writer: &mut W, set_file: &SetFile) -> SetResult<()> {
    let text = build_set_text(set_file)?;
    writer.write_all(text.as_bytes())?;
    Ok(())
}

fn apply_section(set_file: &mut SetFile, section_name: &str, entries: &BTreeMap<String, String>) {
    let section_upper = section_name.to_ascii_uppercase();

    match section_upper.as_str() {
        "GENERAL" => set_file.general = parse_general(entries),
        "GRASS" => set_file.grass = Some(parse_grass(entries)),
        "TERRAIN TYPES" | "CROSSER TYPES" | "PRIMARY RULES" | "SECONDARY RULES" | "TILES"
        | "GROUPS" => {}
        _ => {
            if let Some(index) = parse_indexed_section(&section_upper, "TERRAIN") {
                set_file
                    .terrains
                    .insert(index, parse_named_type(index, entries));
            } else if let Some(index) = parse_indexed_section(&section_upper, "CROSSER") {
                set_file
                    .crossers
                    .insert(index, parse_named_type(index, entries));
            } else if let Some(index) = parse_indexed_section(&section_upper, "GROUP") {
                set_file.groups.insert(index, parse_group(index, entries));
            } else if let Some(index) = parse_indexed_section(&section_upper, "PRIMARY RULE") {
                set_file
                    .primary_rules
                    .insert(index, parse_primary_rule(index, entries));
            } else if let Some((tile_id, door_id)) = parse_tile_door_section(&section_upper) {
                set_file.tile_doors.insert(
                    (tile_id, door_id),
                    parse_tile_door(tile_id, door_id, entries),
                );
            } else if let Some(index) = parse_indexed_section(&section_upper, "TILE") {
                set_file.tiles.insert(index, parse_tile(index, entries));
            }
        }
    }
}

fn parse_general(entries: &BTreeMap<String, String>) -> SetGeneral {
    SetGeneral {
        name:                  read_text(entries, "name"),
        file_type:             read_text(entries, "type"),
        version:               read_text(entries, "version"),
        interior:              read_bool(entries, "interior"),
        has_height_transition: read_bool(entries, "hasheighttransition"),
        env_map:               read_text(entries, "envmap"),
        transition:            read_i32(entries, "transition"),
        selector_height:       read_i32(entries, "selectorheight"),
        display_name:          read_i32(entries, "displayname"),
        unlocalized_name:      read_text(entries, "unlocalizedname"),
        border:                read_text(entries, "border"),
        default_terrain:       read_text(entries, "default"),
        floor:                 read_text(entries, "floor"),
    }
}

fn parse_grass(entries: &BTreeMap<String, String>) -> SetGrass {
    SetGrass {
        grass:        read_bool(entries, "grass"),
        texture_name: read_text(entries, "grasstexturename"),
        density:      read_f32(entries, "density"),
        height:       read_f32(entries, "height"),
        ambient:      parse_rgb(entries, "ambientred", "ambientgreen", "ambientblue"),
        diffuse:      parse_rgb(entries, "diffusered", "diffusegreen", "diffuseblue"),
    }
}

fn parse_named_type(id: u32, entries: &BTreeMap<String, String>) -> SetNamedType {
    SetNamedType {
        id,
        name: read_text(entries, "name"),
        str_ref: read_i32(entries, "strref"),
    }
}

fn parse_group(id: u32, entries: &BTreeMap<String, String>) -> SetGroup {
    let mut tiles = BTreeMap::new();
    for (key, value) in entries {
        if let Some(index) = key
            .strip_prefix("tile")
            .and_then(|suffix| suffix.parse::<u32>().ok())
        {
            tiles.insert(
                index,
                value
                    .parse::<i32>()
                    .ok()
                    .and_then(|raw| u32::try_from(raw).ok()),
            );
        }
    }

    SetGroup {
        id,
        name: read_text(entries, "name"),
        str_ref: read_i32(entries, "strref"),
        rows: read_u32(entries, "rows"),
        columns: read_u32(entries, "columns"),
        tiles,
    }
}

fn parse_primary_rule(id: u32, entries: &BTreeMap<String, String>) -> SetPrimaryRule {
    SetPrimaryRule {
        id,
        placed: read_text(entries, "placed"),
        placed_height: read_i32(entries, "placedheight"),
        adjacent: read_text(entries, "adjacent"),
        adjacent_height: read_i32(entries, "adjacentheight"),
        changed: read_text(entries, "changed"),
        changed_height: read_i32(entries, "changedheight"),
    }
}

fn parse_tile(id: u32, entries: &BTreeMap<String, String>) -> SetTile {
    SetTile {
        id,
        model: read_text(entries, "model"),
        walkmesh: read_text(entries, "walkmesh"),
        top_left: parse_tile_corner(entries, "topleft", "topleftheight"),
        top_right: parse_tile_corner(entries, "topright", "toprightheight"),
        bottom_left: parse_tile_corner(entries, "bottomleft", "bottomleftheight"),
        bottom_right: parse_tile_corner(entries, "bottomright", "bottomrightheight"),
        edge_crossers: SetTileEdges {
            top:    read_text(entries, "top"),
            right:  read_text(entries, "right"),
            bottom: read_text(entries, "bottom"),
            left:   read_text(entries, "left"),
        },
        main_light_1: read_bool(entries, "mainlight1"),
        main_light_2: read_bool(entries, "mainlight2"),
        source_light_1: read_bool(entries, "sourcelight1"),
        source_light_2: read_bool(entries, "sourcelight2"),
        anim_loop_1: read_bool(entries, "animloop1"),
        anim_loop_2: read_bool(entries, "animloop2"),
        anim_loop_3: read_bool(entries, "animloop3"),
        doors: read_u32(entries, "doors"),
        sounds: read_u32(entries, "sounds"),
        path_node: read_text(entries, "pathnode"),
        orientation: read_i32(entries, "orientation"),
        visibility_node: read_text(entries, "visibilitynode"),
        visibility_orientation: read_i32(entries, "visibilityorientation"),
        door_visibility_node: read_text(entries, "doorvisibilitynode"),
        door_visibility_orientation: read_i32(entries, "doorvisibilityorientation"),
        image_map_2d: read_text(entries, "imagemap2d"),
    }
}

fn parse_tile_door(tile_id: u32, door_id: u32, entries: &BTreeMap<String, String>) -> SetTileDoor {
    SetTileDoor {
        tile_id,
        door_id,
        door_type: read_i32(entries, "type"),
        x: read_f32(entries, "x"),
        y: read_f32(entries, "y"),
        z: read_f32(entries, "z"),
        orientation: read_i32(entries, "orientation"),
    }
}

fn parse_tile_corner(
    entries: &BTreeMap<String, String>,
    terrain_key: &str,
    height_key: &str,
) -> SetTileCorner {
    SetTileCorner {
        terrain: read_text(entries, terrain_key)
            .filter(|value| !value.eq_ignore_ascii_case("invalid")),
        height:  read_i32(entries, height_key),
    }
}

fn parse_rgb(
    entries: &BTreeMap<String, String>,
    red_key: &str,
    green_key: &str,
    blue_key: &str,
) -> Option<[f32; 3]> {
    Some([
        read_f32(entries, red_key)?,
        read_f32(entries, green_key)?,
        read_f32(entries, blue_key)?,
    ])
}

fn parse_indexed_section(section_name: &str, prefix: &str) -> Option<u32> {
    let suffix = section_name.strip_prefix(prefix)?;
    if suffix.is_empty() {
        return None;
    }
    suffix.parse::<u32>().ok()
}

fn parse_tile_door_section(section_name: &str) -> Option<(u32, u32)> {
    let (tile_part, door_part) = section_name.split_once("DOOR")?;
    let tile_id = tile_part.strip_prefix("TILE")?.parse::<u32>().ok()?;
    let door_id = door_part.parse::<u32>().ok()?;
    Some((tile_id, door_id))
}

fn read_text(entries: &BTreeMap<String, String>, key: &str) -> Option<String> {
    let value = entries.get(key)?.trim().trim_matches('"');
    if value.is_empty() || value == "****" {
        return None;
    }
    Some(value.to_string())
}

fn read_bool(entries: &BTreeMap<String, String>, key: &str) -> Option<bool> {
    let value = entries.get(key)?.trim();
    match value {
        "1" => Some(true),
        "0" => Some(false),
        _ if value.eq_ignore_ascii_case("true") => Some(true),
        _ if value.eq_ignore_ascii_case("false") => Some(false),
        _ => None,
    }
}

fn read_u32(entries: &BTreeMap<String, String>, key: &str) -> Option<u32> {
    entries.get(key)?.trim().parse::<u32>().ok()
}

fn read_i32(entries: &BTreeMap<String, String>, key: &str) -> Option<i32> {
    entries.get(key)?.trim().parse::<i32>().ok()
}

fn read_f32(entries: &BTreeMap<String, String>, key: &str) -> Option<f32> {
    entries.get(key)?.trim().parse::<f32>().ok()
}

fn push_blank_line(text: &mut String) {
    if !text.is_empty() && !text.ends_with("\n\n") {
        text.push('\n');
    }
}

fn write_section_header(text: &mut String, name: &str) {
    text.push('[');
    text.push_str(name);
    text.push_str("]\n");
}

fn write_string_value(text: &mut String, key: &str, value: Option<&String>) {
    if let Some(value) = value {
        text.push_str(key);
        text.push('=');
        text.push_str(value);
        text.push('\n');
    }
}

fn write_bool_value(text: &mut String, key: &str, value: Option<bool>) {
    if let Some(value) = value {
        text.push_str(key);
        text.push('=');
        text.push_str(if value { "1" } else { "0" });
        text.push('\n');
    }
}

fn write_u32_value(text: &mut String, key: &str, value: Option<u32>) {
    if let Some(value) = value {
        text.push_str(key);
        text.push('=');
        text.push_str(&value.to_string());
        text.push('\n');
    }
}

fn write_i32_value(text: &mut String, key: &str, value: Option<i32>) {
    if let Some(value) = value {
        text.push_str(key);
        text.push('=');
        text.push_str(&value.to_string());
        text.push('\n');
    }
}

fn write_f32_value(text: &mut String, key: &str, value: Option<f32>) {
    if let Some(value) = value {
        text.push_str(key);
        text.push('=');
        text.push_str(&value.to_string());
        text.push('\n');
    }
}

fn write_count_section(text: &mut String, name: &str, count: usize) {
    write_section_header(text, name);
    text.push_str("Count=");
    text.push_str(&count.to_string());
    text.push('\n');
}

fn write_general(text: &mut String, general: &SetGeneral) {
    write_section_header(text, "GENERAL");
    write_string_value(text, "Name", general.name.as_ref());
    write_string_value(text, "Type", general.file_type.as_ref());
    write_string_value(text, "Version", general.version.as_ref());
    write_bool_value(text, "Interior", general.interior);
    write_bool_value(text, "HasHeightTransition", general.has_height_transition);
    write_string_value(text, "EnvMap", general.env_map.as_ref());
    write_i32_value(text, "Transition", general.transition);
    write_i32_value(text, "SelectorHeight", general.selector_height);
    write_i32_value(text, "DisplayName", general.display_name);
    write_string_value(text, "UnlocalizedName", general.unlocalized_name.as_ref());
    write_string_value(text, "Border", general.border.as_ref());
    write_string_value(text, "Default", general.default_terrain.as_ref());
    write_string_value(text, "Floor", general.floor.as_ref());
}

fn write_grass(text: &mut String, grass: &SetGrass) {
    write_section_header(text, "GRASS");
    write_bool_value(text, "Grass", grass.grass);
    write_string_value(text, "GrassTextureName", grass.texture_name.as_ref());
    write_f32_value(text, "Density", grass.density);
    write_f32_value(text, "Height", grass.height);
    if let Some([red, green, blue]) = grass.ambient {
        write_f32_value(text, "AmbientRed", Some(red));
        write_f32_value(text, "AmbientGreen", Some(green));
        write_f32_value(text, "AmbientBlue", Some(blue));
    }
    if let Some([red, green, blue]) = grass.diffuse {
        write_f32_value(text, "DiffuseRed", Some(red));
        write_f32_value(text, "DiffuseGreen", Some(green));
        write_f32_value(text, "DiffuseBlue", Some(blue));
    }
}

fn write_named_type_section(text: &mut String, section_name: &str, named_type: &SetNamedType) {
    write_section_header(text, section_name);
    write_string_value(text, "Name", named_type.name.as_ref());
    write_i32_value(text, "StrRef", named_type.str_ref);
}

fn write_primary_rule_section(
    text: &mut String,
    section_name: &str,
    primary_rule: &SetPrimaryRule,
) {
    write_section_header(text, section_name);
    write_string_value(text, "Placed", primary_rule.placed.as_ref());
    write_i32_value(text, "PlacedHeight", primary_rule.placed_height);
    write_string_value(text, "Adjacent", primary_rule.adjacent.as_ref());
    write_i32_value(text, "AdjacentHeight", primary_rule.adjacent_height);
    write_string_value(text, "Changed", primary_rule.changed.as_ref());
    write_i32_value(text, "ChangedHeight", primary_rule.changed_height);
}

fn write_tile_section(text: &mut String, section_name: &str, tile: &SetTile) {
    write_section_header(text, section_name);
    write_string_value(text, "Model", tile.model.as_ref());
    write_string_value(text, "WalkMesh", tile.walkmesh.as_ref());
    write_string_value(text, "TopLeft", tile.top_left.terrain.as_ref());
    write_i32_value(text, "TopLeftHeight", tile.top_left.height);
    write_string_value(text, "TopRight", tile.top_right.terrain.as_ref());
    write_i32_value(text, "TopRightHeight", tile.top_right.height);
    write_string_value(text, "BottomLeft", tile.bottom_left.terrain.as_ref());
    write_i32_value(text, "BottomLeftHeight", tile.bottom_left.height);
    write_string_value(text, "BottomRight", tile.bottom_right.terrain.as_ref());
    write_i32_value(text, "BottomRightHeight", tile.bottom_right.height);
    write_string_value(text, "Top", tile.edge_crossers.top.as_ref());
    write_string_value(text, "Right", tile.edge_crossers.right.as_ref());
    write_string_value(text, "Bottom", tile.edge_crossers.bottom.as_ref());
    write_string_value(text, "Left", tile.edge_crossers.left.as_ref());
    write_bool_value(text, "MainLight1", tile.main_light_1);
    write_bool_value(text, "MainLight2", tile.main_light_2);
    write_bool_value(text, "SourceLight1", tile.source_light_1);
    write_bool_value(text, "SourceLight2", tile.source_light_2);
    write_bool_value(text, "AnimLoop1", tile.anim_loop_1);
    write_bool_value(text, "AnimLoop2", tile.anim_loop_2);
    write_bool_value(text, "AnimLoop3", tile.anim_loop_3);
    write_u32_value(text, "Doors", tile.doors);
    write_u32_value(text, "Sounds", tile.sounds);
    write_string_value(text, "PathNode", tile.path_node.as_ref());
    write_i32_value(text, "Orientation", tile.orientation);
    write_string_value(text, "VisibilityNode", tile.visibility_node.as_ref());
    write_i32_value(text, "VisibilityOrientation", tile.visibility_orientation);
    write_string_value(
        text,
        "DoorVisibilityNode",
        tile.door_visibility_node.as_ref(),
    );
    write_i32_value(
        text,
        "DoorVisibilityOrientation",
        tile.door_visibility_orientation,
    );
    write_string_value(text, "ImageMap2D", tile.image_map_2d.as_ref());
}

fn write_tile_door_section(text: &mut String, section_name: &str, tile_door: &SetTileDoor) {
    write_section_header(text, section_name);
    write_i32_value(text, "Type", tile_door.door_type);
    write_f32_value(text, "X", tile_door.x);
    write_f32_value(text, "Y", tile_door.y);
    write_f32_value(text, "Z", tile_door.z);
    write_i32_value(text, "Orientation", tile_door.orientation);
}

fn write_group_section(text: &mut String, section_name: &str, group: &SetGroup) {
    write_section_header(text, section_name);
    write_string_value(text, "Name", group.name.as_ref());
    write_i32_value(text, "StrRef", group.str_ref);
    write_u32_value(text, "Rows", group.rows);
    write_u32_value(text, "Columns", group.columns);
    for (index, tile_id) in &group.tiles {
        text.push_str("Tile");
        text.push_str(&index.to_string());
        text.push('=');
        match tile_id {
            Some(tile_id) => text.push_str(&tile_id.to_string()),
            None => text.push_str("-1"),
        }
        text.push('\n');
    }
}

/// Common imports for consumers of this crate.
pub mod prelude {
    pub use crate::{
        SET_RES_TYPE, SetError, SetFile, SetGeneral, SetGrass, SetGroup, SetNamedType,
        SetPrimaryRule, SetResult, SetTile, SetTileCorner, SetTileDoor, SetTileEdges,
        build_set_text, parse_set, read_set, write_set,
    };
}

#[allow(clippy::panic)]
#[cfg(test)]
mod tests {
    use std::{fs, path::PathBuf};

    use super::{SetFile, build_set_text, parse_set, write_set};

    #[test]
    fn parses_minimal_tileset() {
        let parsed = parse_set(
            r#"
                [GENERAL]
                Name=TST01
                Type=SET
                Version=V1.0
                Interior=0

                [TERRAIN TYPES]
                Count=1

                [TERRAIN0]
                Name=Grass
                StrRef=42

                [TILES]
                Count=1

                [TILE0]
                Model=tst01_a01_01
                WalkMesh=msb01
                TopLeft=Grass
                TopLeftHeight=0
                TopRight=Grass
                TopRightHeight=0
                BottomLeft=Grass
                BottomLeftHeight=0
                BottomRight=Grass
                BottomRightHeight=0
                PathNode=A
                Orientation=90
            "#,
        )
        .unwrap_or_else(|error| panic!("parse set: {error}"));

        assert_eq!(parsed.general.name.as_deref(), Some("TST01"));
        assert_eq!(
            parsed
                .terrains
                .get(&0)
                .and_then(|terrain| terrain.name.as_deref()),
            Some("Grass")
        );
        assert_eq!(
            parsed.tiles.get(&0).and_then(|tile| tile.model.as_deref()),
            Some("tst01_a01_01")
        );
        assert_eq!(
            parsed.tiles.get(&0).and_then(|tile| tile.orientation),
            Some(90)
        );
    }

    #[test]
    fn parses_workspace_set_samples() {
        let root = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../set");
        if !root.is_dir() {
            return;
        }
        let entries = fs::read_dir(&root).unwrap_or_else(|error| {
            panic!("read set sample dir {}: {error}", root.display());
        });

        let mut parsed_files = 0_usize;
        for entry in entries {
            let entry = entry.unwrap_or_else(|error| panic!("read dir entry: {error}"));
            let path = entry.path();
            if path.extension().and_then(|ext| ext.to_str()) != Some("set") {
                continue;
            }

            let parsed = SetFile::from_file(&path).unwrap_or_else(|error| {
                panic!("parse {}: {error}", path.display());
            });
            assert!(
                !parsed.tiles.is_empty(),
                "expected at least one tile in {}",
                path.display()
            );
            parsed_files += 1;
        }

        assert!(parsed_files > 0, "expected at least one sample .set file");
    }

    #[test]
    fn builds_and_reparses_structured_tileset() {
        let original = parse_set(
            r#"
                [GENERAL]
                Name=TST01
                Type=SET
                Version=V1.0
                Interior=0
                HasHeightTransition=1

                [GRASS]
                Grass=1
                GrassTextureName=grass01
                Density=1.5
                Height=2
                AmbientRed=0.1
                AmbientGreen=0.2
                AmbientBlue=0.3
                DiffuseRed=0.4
                DiffuseGreen=0.5
                DiffuseBlue=0.6

                [TERRAIN TYPES]
                Count=1

                [TERRAIN0]
                Name=Grass
                StrRef=42

                [CROSSER TYPES]
                Count=1

                [CROSSER0]
                Name=Road

                [PRIMARY RULES]
                Count=1

                [PRIMARY RULE0]
                Placed=Grass
                PlacedHeight=0
                Adjacent=Road
                AdjacentHeight=1
                Changed=Road
                ChangedHeight=2

                [TILES]
                Count=1

                [TILE0]
                Model=tst01_a01_01
                WalkMesh=msb01
                TopLeft=Grass
                TopLeftHeight=0
                TopRight=Grass
                TopRightHeight=1
                BottomLeft=Grass
                BottomLeftHeight=2
                BottomRight=Grass
                BottomRightHeight=3
                Top=Road
                Right=Road
                Bottom=Road
                Left=Road
                MainLight1=1
                SourceLight2=0
                AnimLoop3=1
                Doors=1
                Sounds=2
                PathNode=A
                Orientation=90
                VisibilityNode=V
                VisibilityOrientation=180
                DoorVisibilityNode=D
                DoorVisibilityOrientation=270
                ImageMap2D=tile0

                [TILE0DOOR0]
                Type=3
                X=1
                Y=2
                Z=3
                Orientation=45

                [GROUPS]
                Count=1

                [GROUP0]
                Name=Corner
                StrRef=7
                Rows=1
                Columns=2
                Tile0=0
                Tile1=-1
            "#,
        )
        .unwrap_or_else(|error| panic!("parse set: {error}"));

        let built = build_set_text(&original).unwrap_or_else(|error| panic!("build set: {error}"));
        assert!(built.contains("[TERRAIN TYPES]\nCount=1"));
        assert!(built.contains("[GROUPS]\nCount=1"));

        let reparsed = parse_set(&built).unwrap_or_else(|error| panic!("reparse set: {error}"));
        assert_eq!(reparsed, original);
    }

    #[test]
    fn write_set_matches_build_text() {
        let original = parse_set(
            r#"
                [GENERAL]
                Name=TST01

                [TILES]
                Count=1

                [TILE0]
                Model=tst01_a01_01
            "#,
        )
        .unwrap_or_else(|error| panic!("parse set: {error}"));

        let built = build_set_text(&original).unwrap_or_else(|error| panic!("build set: {error}"));
        let mut bytes = Vec::new();
        write_set(&mut bytes, &original).unwrap_or_else(|error| panic!("write set: {error}"));

        let written =
            String::from_utf8(bytes).unwrap_or_else(|error| panic!("utf8 write set: {error}"));
        assert_eq!(written, built);
        let reparsed = parse_set(&written).unwrap_or_else(|error| panic!("reparse set: {error}"));
        assert_eq!(reparsed, original);
    }

    #[test]
    fn rejects_building_tileset_without_tiles() {
        let error = build_set_text(&SetFile::default())
            .err()
            .unwrap_or_else(|| panic!("expected build error for empty tileset"));
        assert_eq!(
            error.to_string(),
            "cannot build SET payload without at least one tile definition"
        );
    }
}