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
use crate::sound::SoundID;
use crate::sprite::{GraphicID, SpriteID};
use crate::task::TaskList;
use crate::terrain::TerrainID;
use arrayvec::ArrayVec;
use byteorder::{ReadBytesExt, WriteBytesExt, LE};
pub use genie_support::UnitTypeID;
use genie_support::{read_opt_u16, read_opt_u32, MapInto, StringKey, TechID};
use std::convert::TryInto;
use std::io::{self, Read, Result, Write};

pub type UnitClass = u16;

#[derive(Debug, Clone)]
pub enum UnitType {
    /// The base unit type, for units that do not do anything.
    Base(Box<BaseUnitType>),
    /// The tree unit type.
    Tree(Box<TreeUnitType>),
    /// Unit type that supports animated sprites.
    Animated(Box<AnimatedUnitType>),
    /// Unit type for the "fake" units you see in the fog of war, after the actual unit has been
    /// destroyed.
    Doppleganger(Box<DopplegangerUnitType>),
    /// Unit type that supports movement.
    Moving(Box<MovingUnitType>),
    /// Unit type that supports being tasked by a player.
    Action(Box<ActionUnitType>),
    /// Unit type that supports combat.
    BaseCombat(Box<BaseCombatUnitType>),
    /// Unit type for projectiles/missiles/arrows.
    Missile(Box<MissileUnitType>),
    /// Unit type that supports combat (with additional Age of Empires specific data).
    Combat(Box<CombatUnitType>),
    /// Unit type for buildings.
    Building(Box<BuildingUnitType>),
}

macro_rules! cast_unit_type {
    ($struct:ident, $tag:ident) => {
        impl From<$struct> for UnitType {
            fn from(v: $struct) -> Self {
                UnitType::$tag(Box::new(v))
            }
        }
    };
}

macro_rules! inherit_unit_type {
    ($struct:ident, $super:ident) => {
        impl std::ops::Deref for $struct {
            type Target = $super;
            fn deref(&self) -> &Self::Target {
                &self.superclass
            }
        }
    };
}

cast_unit_type!(BaseUnitType, Base);
cast_unit_type!(TreeUnitType, Tree);
cast_unit_type!(AnimatedUnitType, Animated);
cast_unit_type!(DopplegangerUnitType, Doppleganger);
cast_unit_type!(MovingUnitType, Moving);
cast_unit_type!(ActionUnitType, Action);
cast_unit_type!(BaseCombatUnitType, BaseCombat);
cast_unit_type!(MissileUnitType, Missile);
cast_unit_type!(CombatUnitType, Combat);
cast_unit_type!(BuildingUnitType, Building);

// inherit_unit_type!(TreeUnitType, BaseUnitType);
inherit_unit_type!(AnimatedUnitType, BaseUnitType);
// inherit_unit_type!(DopplegangerUnitType, AnimatedUnitType);
inherit_unit_type!(MovingUnitType, AnimatedUnitType);
inherit_unit_type!(ActionUnitType, MovingUnitType);
inherit_unit_type!(BaseCombatUnitType, ActionUnitType);
inherit_unit_type!(MissileUnitType, BaseCombatUnitType);
inherit_unit_type!(CombatUnitType, BaseCombatUnitType);
inherit_unit_type!(BuildingUnitType, CombatUnitType);

impl UnitType {
    fn type_id(&self) -> u8 {
        use UnitType::*;
        match self {
            Base(_) => 10,
            Tree(_) => 15,
            Animated(_) => 20,
            Doppleganger(_) => 25,
            Moving(_) => 30,
            Action(_) => 40,
            BaseCombat(_) => 50,
            Missile(_) => 60,
            Combat(_) => 70,
            Building(_) => 80,
        }
    }

    /// Read a unit type from an input stream.
    pub fn read_from(mut input: impl Read, version: f32) -> Result<Self> {
        let unit_type = input.read_u8()?;
        match unit_type {
            10 => BaseUnitType::read_from(input, version).map_into(),
            15 => TreeUnitType::read_from(input, version).map_into(),
            20 => AnimatedUnitType::read_from(input, version).map_into(),
            25 => DopplegangerUnitType::read_from(input, version).map_into(),
            30 => MovingUnitType::read_from(input, version).map_into(),
            40 => ActionUnitType::read_from(input, version).map_into(),
            50 => BaseCombatUnitType::read_from(input, version).map_into(),
            60 => MissileUnitType::read_from(input, version).map_into(),
            70 => CombatUnitType::read_from(input, version).map_into(),
            80 => BuildingUnitType::read_from(input, version).map_into(),
            _ => panic!("unexpected unit type {}, this is probably a bug", unit_type),
        }
    }

    /// Write this unit type to an output stream.
    pub fn write_to(&self, mut output: impl Write, version: f32) -> Result<()> {
        use UnitType::*;
        output.write_u8(self.type_id())?;

        match self {
            Base(unit) => unit.write_to(output, version)?,
            Tree(unit) => unit.write_to(output, version)?,
            Animated(unit) => unit.write_to(output, version)?,
            Doppleganger(unit) => unit.write_to(output, version)?,
            Moving(unit) => unit.write_to(output, version)?,
            Action(unit) => unit.write_to(output, version)?,
            BaseCombat(unit) => unit.write_to(output, version)?,
            Missile(unit) => unit.write_to(output, version)?,
            Combat(unit) => unit.write_to(output, version)?,
            Building(unit) => unit.write_to(output, version)?,
        }

        Ok(())
    }

    /// Get the base unit type properties for this unit.
    pub fn base(&self) -> &BaseUnitType {
        use UnitType::*;
        match self {
            Base(unit) => &unit,
            Tree(unit) => &unit.0,
            Animated(unit) => &unit,
            Doppleganger(unit) => &unit.0,
            Moving(unit) => &unit,
            Action(unit) => &unit,
            BaseCombat(unit) => &unit,
            Missile(unit) => &unit,
            Combat(unit) => &unit,
            Building(unit) => &unit,
        }
    }

    /// Get the animated unit type properties for this unit.
    pub fn animated(&self) -> Option<&AnimatedUnitType> {
        use UnitType::*;
        match self {
            Base(_) | Tree(_) => None,
            Animated(unit) => Some(&unit),
            Doppleganger(unit) => Some(&unit.0),
            Moving(unit) => Some(&unit),
            Action(unit) => Some(&unit),
            BaseCombat(unit) => Some(&unit),
            Missile(unit) => Some(&unit),
            Combat(unit) => Some(&unit),
            Building(unit) => Some(&unit),
        }
    }

    /// Get the moving unit type properties for this unit.
    pub fn moving(&self) -> Option<&MovingUnitType> {
        use UnitType::*;
        match self {
            Base(_) | Tree(_) | Animated(_) | Doppleganger(_) => None,
            Moving(unit) => Some(&unit),
            Action(unit) => Some(&unit),
            BaseCombat(unit) => Some(&unit),
            Missile(unit) => Some(&unit),
            Combat(unit) => Some(&unit),
            Building(unit) => Some(&unit),
        }
    }

    /// Get the action unit type properties for this unit.
    pub fn action(&self) -> Option<&ActionUnitType> {
        use UnitType::*;
        match self {
            Base(_) | Tree(_) | Animated(_) | Doppleganger(_) | Moving(_) => None,
            Action(unit) => Some(&unit),
            BaseCombat(unit) => Some(&unit),
            Missile(unit) => Some(&unit),
            Combat(unit) => Some(&unit),
            Building(unit) => Some(&unit),
        }
    }
}

#[derive(Debug, Default, Clone, Copy)]
pub struct UnitAttribute {
    pub attribute_type: u16,
    pub amount: f32,
    pub flag: u8,
}

impl UnitAttribute {
    pub fn read_from(mut input: impl Read) -> Result<Self> {
        Ok(Self {
            attribute_type: input.read_u16::<LE>()?,
            amount: input.read_f32::<LE>()?,
            flag: input.read_u8()?,
        })
    }

    pub fn write_to(self, mut output: impl Write) -> Result<()> {
        output.write_u16::<LE>(self.attribute_type)?;
        output.write_f32::<LE>(self.amount)?;
        output.write_u8(self.flag)?;
        Ok(())
    }

    fn write_empty(mut output: impl Write) -> Result<()> {
        output.write_u16::<LE>(0xFFFF)?;
        output.write_f32::<LE>(0.0)?;
        output.write_u8(0)?;
        Ok(())
    }
}

#[derive(Debug, Default, Clone)]
pub struct DamageSprite {
    pub sprite: SpriteID,
    pub damage_percent: u16,
    pub flag: u8,
}

impl DamageSprite {
    pub fn read_from(mut input: impl Read) -> Result<Self> {
        Ok(Self {
            sprite: input.read_u16::<LE>()?.into(),
            damage_percent: input.read_u16::<LE>()?,
            flag: input.read_u8()?,
        })
    }

    pub fn write_to(&self, mut output: impl Write) -> Result<()> {
        output.write_u16::<LE>(self.sprite.into())?;
        output.write_u16::<LE>(self.damage_percent)?;
        output.write_u8(self.flag)?;
        Ok(())
    }
}

#[derive(Debug, Default, Clone)]
pub struct BaseUnitType {
    name: String,
    pub id: UnitTypeID,
    pub string_id: StringKey,
    string_id2: Option<StringKey>,
    pub unit_class: UnitClass,
    pub standing_sprite_1: Option<SpriteID>,
    pub standing_sprite_2: Option<SpriteID>,
    pub dying_sprite: Option<SpriteID>,
    pub undead_sprite: Option<SpriteID>,
    pub undead_flag: u8,
    pub hp: u16,
    pub los: f32,
    pub garrison_capacity: u8,
    pub radius: (f32, f32, f32),
    pub train_sound: Option<SoundID>,
    pub damage_sound: Option<SoundID>,
    pub death_spawn: Option<UnitTypeID>,
    pub sort_number: u8,
    pub can_be_built_on: bool,
    pub button_picture: Option<GraphicID>,
    pub hide_in_scenario_editor: bool,
    pub portrait_picture: Option<GraphicID>,
    pub enabled: bool,
    pub disabled: bool,
    pub tile_req: (i16, i16),
    pub center_tile_req: (i16, i16),
    pub construction_radius: (f32, f32),
    pub elevation_flag: bool,
    pub fog_flag: bool,
    pub terrain_restriction_id: u16,
    pub movement_type: u8,
    pub attribute_max_amount: u16,
    pub attribute_rot: f32,
    pub area_effect_level: u8,
    pub combat_level: u8,
    pub select_level: u8,
    pub map_draw_level: u8,
    pub unit_level: u8,
    pub multiple_attribute_mod: f32,
    pub map_color: u8,
    pub help_string_id: StringKey,
    pub help_page_id: u32,
    pub hotkey_id: u32,
    pub recyclable: bool,
    pub track_as_resource: bool,
    pub create_doppleganger: bool,
    pub resource_group: u8,
    pub occlusion_mask: u8,
    pub obstruction_type: u8,
    pub selection_shape: u8,
    pub object_flags: u32,
    pub civilization: u8,
    pub attribute_piece: u8,
    pub outline_radius: (f32, f32, f32),
    pub attributes: ArrayVec<[UnitAttribute; 3]>,
    pub damage_sprites: Vec<DamageSprite>,
    pub selected_sound: Option<SoundID>,
    pub death_sound: Option<SoundID>,
    pub attack_reaction: u8,
    pub convert_terrain_flag: u8,
    pub copy_id: u16,
    pub unit_group: u16,
}

impl BaseUnitType {
    pub fn read_from(mut input: impl Read, version: f32) -> Result<Self> {
        let mut unit_type = Self::default();
        let name_len = input.read_u16::<LE>()?;
        unit_type.id = input.read_u16::<LE>()?.into();
        unit_type.string_id = input.read_u16::<LE>()?.into();
        unit_type.string_id2 = read_opt_u16(&mut input)?;
        unit_type.unit_class = input.read_u16::<LE>()?;
        unit_type.standing_sprite_1 = read_opt_u16(&mut input)?;
        unit_type.standing_sprite_2 = read_opt_u16(&mut input)?;
        unit_type.dying_sprite = read_opt_u16(&mut input)?;
        unit_type.undead_sprite = read_opt_u16(&mut input)?;
        unit_type.undead_flag = input.read_u8()?;
        unit_type.hp = input.read_u16::<LE>()?;
        unit_type.los = input.read_f32::<LE>()?;
        unit_type.garrison_capacity = input.read_u8()?;
        unit_type.radius = (
            input.read_f32::<LE>()?,
            input.read_f32::<LE>()?,
            input.read_f32::<LE>()?,
        );
        unit_type.train_sound = read_opt_u16(&mut input)?;
        unit_type.damage_sound = read_opt_u16(&mut input)?;
        unit_type.death_spawn = read_opt_u16(&mut input)?;
        unit_type.sort_number = input.read_u8()?;
        unit_type.can_be_built_on = input.read_u8()? != 0;
        unit_type.button_picture = read_opt_u16(&mut input)?;
        unit_type.hide_in_scenario_editor = input.read_u8()? != 0;
        unit_type.portrait_picture = read_opt_u16(&mut input)?;
        unit_type.enabled = input.read_u8()? != 0;
        unit_type.disabled = input.read_u8()? != 0;
        unit_type.tile_req = (input.read_i16::<LE>()?, input.read_i16::<LE>()?);
        unit_type.center_tile_req = (input.read_i16::<LE>()?, input.read_i16::<LE>()?);
        unit_type.construction_radius = (input.read_f32::<LE>()?, input.read_f32::<LE>()?);
        unit_type.elevation_flag = input.read_u8()? != 0;
        unit_type.fog_flag = input.read_u8()? != 0;
        unit_type.terrain_restriction_id = input.read_u16::<LE>()?;
        unit_type.movement_type = input.read_u8()?;
        unit_type.attribute_max_amount = input.read_u16::<LE>()?;
        unit_type.attribute_rot = input.read_f32::<LE>()?;
        unit_type.area_effect_level = input.read_u8()?;
        unit_type.combat_level = input.read_u8()?;
        unit_type.select_level = input.read_u8()?;
        unit_type.map_draw_level = input.read_u8()?;
        unit_type.unit_level = input.read_u8()?;
        unit_type.multiple_attribute_mod = input.read_f32::<LE>()?;
        unit_type.map_color = input.read_u8()?;
        unit_type.help_string_id = input.read_u32::<LE>()?.into();
        unit_type.help_page_id = input.read_u32::<LE>()?;
        unit_type.hotkey_id = input.read_u32::<LE>()?;
        unit_type.recyclable = input.read_u8()? != 0;
        unit_type.track_as_resource = input.read_u8()? != 0;
        unit_type.create_doppleganger = input.read_u8()? != 0;
        unit_type.resource_group = input.read_u8()?;
        unit_type.occlusion_mask = input.read_u8()?;
        unit_type.obstruction_type = input.read_u8()?;
        unit_type.selection_shape = input.read_u8()?;
        unit_type.object_flags = if version < 11.55 {
            0
        } else {
            input.read_u32::<LE>()?
        };
        unit_type.civilization = input.read_u8()?;
        unit_type.attribute_piece = input.read_u8()?;
        unit_type.outline_radius = (
            input.read_f32::<LE>()?,
            input.read_f32::<LE>()?,
            input.read_f32::<LE>()?,
        );
        for _ in 0..3 {
            let attr = UnitAttribute::read_from(&mut input)?;
            if attr.attribute_type != 0xFFFF {
                unit_type.attributes.push(attr);
            }
        }
        unit_type.damage_sprites = {
            let num_damage_sprites = input.read_u8()?;
            let mut damage_sprites = vec![];
            for _ in 0..num_damage_sprites {
                damage_sprites.push(DamageSprite::read_from(&mut input)?);
            }
            damage_sprites
        };
        unit_type.selected_sound = read_opt_u16(&mut input)?;
        unit_type.death_sound = read_opt_u16(&mut input)?;
        unit_type.attack_reaction = input.read_u8()?;
        unit_type.convert_terrain_flag = input.read_u8()?;
        unit_type.name = {
            // TODO use not-UTF8 for the name
            let mut bytes = vec![0; usize::from(name_len)];
            input.read_exact(&mut bytes)?;
            String::from_utf8(bytes.iter().cloned().take_while(|b| *b != 0).collect()).unwrap()
        };
        unit_type.copy_id = input.read_u16::<LE>()?;
        unit_type.unit_group = input.read_u16::<LE>()?;
        Ok(unit_type)
    }

    /// Write this unit type to an output stream.
    pub fn write_to(&self, mut output: impl Write, _version: f32) -> Result<()> {
        // TODO use not-UTF8 for the name
        output.write_u16::<LE>(self.name.len() as u16)?;
        output.write_u16::<LE>(self.id.into())?;
        output.write_i16::<LE>((&self.string_id).try_into().unwrap())?;
        write_opt_string_key(&mut output, &self.string_id2)?;
        output.write_u16::<LE>(self.unit_class)?;
        output.write_i16::<LE>(
            self.standing_sprite_1
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_i16::<LE>(
            self.standing_sprite_2
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_i16::<LE>(
            self.dying_sprite
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_i16::<LE>(
            self.undead_sprite
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_u8(self.undead_flag)?;
        output.write_u16::<LE>(self.hp)?;
        output.write_f32::<LE>(self.los)?;
        output.write_u8(self.garrison_capacity)?;
        output.write_f32::<LE>(self.radius.0)?;
        output.write_f32::<LE>(self.radius.1)?;
        output.write_f32::<LE>(self.radius.2)?;
        output.write_i16::<LE>(
            self.train_sound
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_i16::<LE>(
            self.damage_sound
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_i16::<LE>(
            self.death_spawn
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_u8(self.sort_number)?;
        output.write_u8(if self.can_be_built_on { 1 } else { 0 })?;
        output.write_i16::<LE>(
            self.button_picture
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_u8(if self.hide_in_scenario_editor { 1 } else { 0 })?;
        output.write_i16::<LE>(
            self.portrait_picture
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_u8(if self.enabled { 1 } else { 0 })?;
        output.write_u8(if self.disabled { 1 } else { 0 })?;
        output.write_i16::<LE>(self.tile_req.0)?;
        output.write_i16::<LE>(self.tile_req.1)?;
        output.write_i16::<LE>(self.center_tile_req.0)?;
        output.write_i16::<LE>(self.center_tile_req.1)?;
        output.write_f32::<LE>(self.construction_radius.0)?;
        output.write_f32::<LE>(self.construction_radius.1)?;
        output.write_u8(if self.elevation_flag { 1 } else { 0 })?;
        output.write_u8(if self.fog_flag { 1 } else { 0 })?;
        output.write_u16::<LE>(self.terrain_restriction_id)?;
        output.write_u8(self.movement_type)?;
        output.write_u16::<LE>(self.attribute_max_amount)?;
        output.write_f32::<LE>(self.attribute_rot)?;
        output.write_u8(self.area_effect_level)?;
        output.write_u8(self.combat_level)?;
        output.write_u8(self.select_level)?;
        output.write_u8(self.map_draw_level)?;
        output.write_u8(self.unit_level)?;
        output.write_f32::<LE>(self.multiple_attribute_mod)?;
        output.write_u8(self.map_color)?;
        output.write_u32::<LE>((&self.help_string_id).try_into().unwrap())?;
        output.write_u32::<LE>(self.help_page_id)?;
        output.write_u32::<LE>(self.hotkey_id)?;
        output.write_u8(if self.recyclable { 1 } else { 0 })?;
        output.write_u8(if self.track_as_resource { 1 } else { 0 })?;
        output.write_u8(if self.create_doppleganger { 1 } else { 0 })?;
        output.write_u8(self.resource_group)?;
        output.write_u8(self.occlusion_mask)?;
        output.write_u8(self.obstruction_type)?;
        output.write_u8(self.selection_shape)?;
        output.write_u32::<LE>(self.object_flags)?;
        output.write_u8(self.civilization)?;
        output.write_u8(self.attribute_piece)?;
        output.write_f32::<LE>(self.outline_radius.0)?;
        output.write_f32::<LE>(self.outline_radius.1)?;
        output.write_f32::<LE>(self.outline_radius.2)?;
        for index in 0..self.attributes.capacity() {
            match self.attributes.get(index) {
                Some(attr) => attr.write_to(&mut output)?,
                None => UnitAttribute::write_empty(&mut output)?,
            }
        }
        output.write_u8(self.damage_sprites.len().try_into().unwrap())?;
        for sprite in &self.damage_sprites {
            sprite.write_to(&mut output)?;
        }
        output.write_i16::<LE>(
            self.selected_sound
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_i16::<LE>(
            self.death_sound
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_u8(self.attack_reaction)?;
        output.write_u8(self.convert_terrain_flag)?;
        output.write_all(self.name.as_bytes())?;
        output.write_u16::<LE>(self.copy_id)?;
        output.write_u16::<LE>(self.unit_group)?;
        Ok(())
    }
}

#[derive(Debug, Default, Clone)]
pub struct TreeUnitType(BaseUnitType);

impl TreeUnitType {
    pub fn read_from(input: impl Read, version: f32) -> Result<Self> {
        BaseUnitType::read_from(input, version).map(Self)
    }

    /// Write this unit type to an output stream.
    pub fn write_to(&self, output: impl Write, version: f32) -> Result<()> {
        self.0.write_to(output, version)
    }
}

#[derive(Debug, Default, Clone)]
pub struct AnimatedUnitType {
    superclass: BaseUnitType,
    pub speed: f32,
}

impl AnimatedUnitType {
    pub fn read_from(mut input: impl Read, version: f32) -> Result<Self> {
        Ok(Self {
            superclass: BaseUnitType::read_from(&mut input, version)?,
            speed: input.read_f32::<LE>()?,
        })
    }

    /// Write this unit type to an output stream.
    pub fn write_to(&self, mut output: impl Write, version: f32) -> Result<()> {
        self.superclass.write_to(&mut output, version)?;
        output.write_f32::<LE>(self.speed)?;
        Ok(())
    }
}

#[derive(Debug, Default, Clone)]
pub struct DopplegangerUnitType(AnimatedUnitType);

impl DopplegangerUnitType {
    pub fn read_from(input: impl Read, version: f32) -> Result<Self> {
        AnimatedUnitType::read_from(input, version).map(Self)
    }

    /// Write this unit type to an output stream.
    pub fn write_to(&self, output: impl Write, version: f32) -> Result<()> {
        self.0.write_to(output, version)
    }
}

#[derive(Debug, Default, Clone)]
pub struct MovingUnitType {
    superclass: AnimatedUnitType,
    pub move_sprite: Option<SpriteID>,
    pub run_sprite: Option<SpriteID>,
    pub turn_speed: f32,
    pub size_class: u8,
    pub trailing_unit: Option<UnitTypeID>,
    pub trailing_options: u8,
    pub trailing_spacing: f32,
    pub move_algorithm: u8,
    pub turn_radius: f32,
    pub turn_radius_speed: f32,
    pub maximum_yaw_per_second_moving: f32,
    pub stationary_yaw_revolution_time: f32,
    pub maximum_yaw_per_second_stationary: f32,
}

impl MovingUnitType {
    pub fn read_from(mut input: impl Read, version: f32) -> Result<Self> {
        let mut unit_type = Self {
            superclass: AnimatedUnitType::read_from(&mut input, version)?,
            ..Default::default()
        };
        unit_type.move_sprite = read_opt_u16(&mut input)?;
        unit_type.run_sprite = read_opt_u16(&mut input)?;
        unit_type.turn_speed = input.read_f32::<LE>()?;
        unit_type.size_class = input.read_u8()?;
        unit_type.trailing_unit = read_opt_u16(&mut input)?;
        unit_type.trailing_options = input.read_u8()?;
        unit_type.trailing_spacing = input.read_f32::<LE>()?;
        unit_type.move_algorithm = input.read_u8()?;
        unit_type.turn_radius = input.read_f32::<LE>()?;
        unit_type.turn_radius_speed = input.read_f32::<LE>()?;
        unit_type.maximum_yaw_per_second_moving = input.read_f32::<LE>()?;
        unit_type.stationary_yaw_revolution_time = input.read_f32::<LE>()?;
        unit_type.maximum_yaw_per_second_stationary = input.read_f32::<LE>()?;
        Ok(unit_type)
    }

    /// Write this unit type to an output stream.
    pub fn write_to(&self, mut output: impl Write, version: f32) -> Result<()> {
        self.superclass.write_to(&mut output, version)?;
        output.write_i16::<LE>(
            self.move_sprite
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_i16::<LE>(
            self.run_sprite
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_f32::<LE>(self.turn_speed)?;
        output.write_u8(self.size_class)?;
        output.write_i16::<LE>(
            self.trailing_unit
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_u8(self.trailing_options)?;
        output.write_f32::<LE>(self.trailing_spacing)?;
        output.write_u8(self.move_algorithm)?;
        output.write_f32::<LE>(self.turn_radius)?;
        output.write_f32::<LE>(self.turn_radius_speed)?;
        output.write_f32::<LE>(self.maximum_yaw_per_second_moving)?;
        output.write_f32::<LE>(self.stationary_yaw_revolution_time)?;
        output.write_f32::<LE>(self.maximum_yaw_per_second_stationary)?;
        Ok(())
    }
}

#[derive(Debug, Default, Clone)]
pub struct ActionUnitType {
    superclass: MovingUnitType,
    pub default_task: Option<u16>,
    pub search_radius: f32,
    pub work_rate: f32,
    pub drop_site: Option<UnitTypeID>,
    pub backup_drop_site: Option<UnitTypeID>,
    pub task_by_group: u8,
    pub command_sound: Option<SoundID>,
    pub move_sound: Option<SoundID>,
    /// Task list for older versions; newer game versions store the task list at the root of the
    /// dat file, and use `unit_type.copy_id` to refer to one of those task lists.
    pub tasks: Option<TaskList>,
    pub run_pattern: u8,
}

impl ActionUnitType {
    pub fn read_from(mut input: impl Read, version: f32) -> Result<Self> {
        let mut unit_type = Self {
            superclass: MovingUnitType::read_from(&mut input, version)?,
            ..Default::default()
        };
        unit_type.default_task = read_opt_u16(&mut input)?;
        unit_type.search_radius = input.read_f32::<LE>()?;
        unit_type.work_rate = input.read_f32::<LE>()?;
        unit_type.drop_site = read_opt_u16(&mut input)?;
        unit_type.backup_drop_site = read_opt_u16(&mut input)?;
        unit_type.task_by_group = input.read_u8()?;
        unit_type.command_sound = read_opt_u16(&mut input)?;
        unit_type.move_sound = read_opt_u16(&mut input)?;
        unit_type.run_pattern = input.read_u8()?;
        Ok(unit_type)
    }

    /// Write this unit type to an output stream.
    pub fn write_to(&self, mut output: impl Write, version: f32) -> Result<()> {
        self.superclass.write_to(&mut output, version)?;
        output.write_i16::<LE>(
            self.default_task
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_f32::<LE>(self.search_radius)?;
        output.write_f32::<LE>(self.work_rate)?;
        output.write_i16::<LE>(
            self.drop_site
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_i16::<LE>(
            self.backup_drop_site
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_u8(self.task_by_group)?;
        output.write_i16::<LE>(
            self.command_sound
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_i16::<LE>(
            self.move_sound
                .map(|id| id.try_into().unwrap())
                .unwrap_or(-1),
        )?;
        output.write_u8(self.run_pattern)?;
        Ok(())
    }
}

#[derive(Debug, Default, Clone, Copy)]
pub struct WeaponInfo {
    pub weapon_type: i16,
    pub value: i16,
}

impl WeaponInfo {
    pub fn read_from(mut input: impl Read) -> Result<Self> {
        Ok(Self {
            weapon_type: input.read_i16::<LE>()?,
            value: input.read_i16::<LE>()?,
        })
    }
    pub fn write_to(self, mut output: impl Write) -> Result<()> {
        output.write_i16::<LE>(self.weapon_type)?;
        output.write_i16::<LE>(self.value)?;
        Ok(())
    }
}

#[derive(Debug, Default, Clone)]
pub struct BaseCombatUnitType {
    superclass: ActionUnitType,
    pub base_armor: u16,
    pub weapons: Vec<WeaponInfo>,
    pub armors: Vec<WeaponInfo>,
    pub defense_terrain_bonus: Option<u16>,
    pub weapon_range_max: f32,
    pub area_effect_range: f32,
    pub attack_speed: f32,
    pub missile_id: Option<UnitTypeID>,
    pub base_hit_chance: i16,
    pub break_off_combat: i8,
    pub frame_delay: i16,
    pub weapon_offset: (f32, f32, f32),
    pub blast_level_offense: i8,
    pub weapon_range_min: f32,
    pub missed_missile_spread: f32,
    pub fight_sprite: Option<SpriteID>,
    pub displayed_armor: i16,
    pub displayed_attack: i16,
    pub displayed_range: f32,
    pub displayed_reload_time: f32,
}

impl BaseCombatUnitType {
    pub fn read_from(mut input: impl Read, version: f32) -> Result<Self> {
        let mut unit_type = Self {
            superclass: ActionUnitType::read_from(&mut input, version)?,
            ..Default::default()
        };
        unit_type.base_armor = if version < 11.52 {
            input.read_u8()?.into()
        } else {
            input.read_u16::<LE>()?
        };
        let num_weapons = input.read_u16::<LE>()?;
        for _ in 0..num_weapons {
            unit_type.weapons.push(WeaponInfo::read_from(&mut input)?);
        }
        let num_armors = input.read_u16::<LE>()?;
        for _ in 0..num_armors {
            unit_type.armors.push(WeaponInfo::read_from(&mut input)?);
        }
        unit_type.defense_terrain_bonus = read_opt_u16(&mut input)?;
        unit_type.weapon_range_max = input.read_f32::<LE>()?;
        unit_type.area_effect_range = input.read_f32::<LE>()?;
        unit_type.attack_speed = input.read_f32::<LE>()?;
        unit_type.missile_id = read_opt_u16(&mut input)?;
        unit_type.base_hit_chance = input.read_i16::<LE>()?;
        unit_type.break_off_combat = input.read_i8()?;
        unit_type.frame_delay = input.read_i16::<LE>()?;
        unit_type.weapon_offset = (
            input.read_f32::<LE>()?,
            input.read_f32::<LE>()?,
            input.read_f32::<LE>()?,
        );
        unit_type.blast_level_offense = input.read_i8()?;
        unit_type.weapon_range_min = input.read_f32::<LE>()?;
        unit_type.missed_missile_spread = input.read_f32::<LE>()?;
        unit_type.fight_sprite = read_opt_u16(&mut input)?;
        unit_type.displayed_armor = input.read_i16::<LE>()?;
        unit_type.displayed_attack = input.read_i16::<LE>()?;
        unit_type.displayed_range = input.read_f32::<LE>()?;
        unit_type.displayed_reload_time = input.read_f32::<LE>()?;
        Ok(unit_type)
    }

    /// Write this unit type to an output stream.
    pub fn write_to(&self, mut output: impl Write, version: f32) -> Result<()> {
        self.superclass.write_to(&mut output, version)?;
        if version < 11.52 {
            output.write_u8(self.base_armor.try_into().unwrap())?;
        } else {
            output.write_u16::<LE>(self.base_armor)?;
        };
        output.write_u16::<LE>(self.weapons.len() as u16)?;
        for weapon in &self.weapons {
            weapon.write_to(&mut output)?;
        }
        output.write_u16::<LE>(self.armors.len() as u16)?;
        for armor in &self.armors {
            armor.write_to(&mut output)?;
        }
        output.write_u16::<LE>(self.defense_terrain_bonus.unwrap_or(0xFFFF))?;
        output.write_f32::<LE>(self.weapon_range_max)?;
        output.write_f32::<LE>(self.area_effect_range)?;
        output.write_f32::<LE>(self.attack_speed)?;
        output.write_u16::<LE>(self.missile_id.map_into().unwrap_or(0xFFFF))?;
        output.write_i16::<LE>(self.base_hit_chance)?;
        output.write_i8(self.break_off_combat)?;
        output.write_i16::<LE>(self.frame_delay)?;
        output.write_f32::<LE>(self.weapon_offset.0)?;
        output.write_f32::<LE>(self.weapon_offset.1)?;
        output.write_f32::<LE>(self.weapon_offset.2)?;
        output.write_i8(self.blast_level_offense)?;
        output.write_f32::<LE>(self.weapon_range_min)?;
        output.write_f32::<LE>(self.missed_missile_spread)?;
        output.write_u16::<LE>(self.fight_sprite.map_into().unwrap_or(0xFFFF))?;
        output.write_i16::<LE>(self.displayed_armor)?;
        output.write_i16::<LE>(self.displayed_attack)?;
        output.write_f32::<LE>(self.displayed_range)?;
        output.write_f32::<LE>(self.displayed_reload_time)?;
        Ok(())
    }
}

#[derive(Debug, Default, Clone)]
pub struct MissileUnitType {
    superclass: BaseCombatUnitType,
    pub missile_type: u8,
    pub targetting_type: u8,
    pub missile_hit_info: u8,
    pub missile_die_info: u8,
    pub area_effect_specials: u8,
    pub ballistics_ratio: f32,
}

impl MissileUnitType {
    /// Read this unit type from an input stream.
    pub fn read_from(mut input: impl Read, version: f32) -> Result<Self> {
        let mut unit_type = Self {
            superclass: BaseCombatUnitType::read_from(&mut input, version)?,
            ..Default::default()
        };
        unit_type.missile_type = input.read_u8()?;
        unit_type.targetting_type = input.read_u8()?;
        unit_type.missile_hit_info = input.read_u8()?;
        unit_type.missile_die_info = input.read_u8()?;
        unit_type.area_effect_specials = input.read_u8()?;
        unit_type.ballistics_ratio = input.read_f32::<LE>()?;
        Ok(unit_type)
    }

    /// Write this unit type to an output stream.
    pub fn write_to(&self, mut output: impl Write, version: f32) -> Result<()> {
        self.superclass.write_to(&mut output, version)?;
        output.write_u8(self.missile_type)?;
        output.write_u8(self.targetting_type)?;
        output.write_u8(self.missile_hit_info)?;
        output.write_u8(self.missile_die_info)?;
        output.write_u8(self.area_effect_specials)?;
        output.write_f32::<LE>(self.ballistics_ratio)?;
        Ok(())
    }
}

/// Resource cost for a unit.
#[derive(Debug, Default, Clone, Copy)]
pub struct AttributeCost {
    /// The player attribute type to give/take.
    pub attribute_type: i16,
    /// The amount of that attribute that should be taken/given.
    pub amount: i16,
    /// Flag determining how and when this cost is counted.
    ///
    /// TODO make this an enum
    pub flag: u8,
}

impl AttributeCost {
    pub fn read_from(mut input: impl Read) -> Result<Self> {
        let cost = Self {
            attribute_type: input.read_i16::<LE>()?,
            amount: input.read_i16::<LE>()?,
            flag: input.read_u8()?,
        };
        let _padding = input.read_u8()?;
        Ok(cost)
    }
    pub fn write_to(self, mut output: impl Write) -> Result<()> {
        output.write_i16::<LE>(self.attribute_type)?;
        output.write_i16::<LE>(self.amount)?;
        output.write_u8(self.flag)?;
        output.write_u8(0)?;
        Ok(())
    }
}

#[derive(Debug, Default, Clone)]
pub struct CombatUnitType {
    superclass: BaseCombatUnitType,
    /// The costs of creating a unit of this type.
    pub costs: ArrayVec<[AttributeCost; 3]>,
    pub create_time: u16,
    /// Unit type ID of the building or unit where this unit can be created.
    pub create_at_building: Option<UnitTypeID>,
    /// Button location index where the button to create this unit should be shown when a
    /// `create_at_building` unit is selected.
    pub create_button: i8,
    pub rear_attack_modifier: f32,
    pub flank_attack_modifier: f32,
    /// Is this unit a hero unit?
    ///
    /// TODO what is special about hero units? Does it just opt into the healing behaviour?
    pub hero_flag: u8,
    pub garrison_sprite: Option<SpriteID>,
    pub volley_fire_amount: f32,
    pub max_attacks_in_volley: i8,
    pub volley_spread: (f32, f32),
    pub volley_start_spread_adjustment: f32,
    pub volley_missile: Option<UnitTypeID>,
    pub special_attack_sprite: Option<SpriteID>,
    pub special_attack_flag: i8,
    pub displayed_pierce_armor: i16,
}

impl CombatUnitType {
    /// Read this unit type from an input stream.
    pub fn read_from(mut input: impl Read, version: f32) -> Result<Self> {
        let mut unit_type = Self {
            superclass: BaseCombatUnitType::read_from(&mut input, version)?,
            ..Default::default()
        };

        for _ in 0..3 {
            let attr = AttributeCost::read_from(&mut input)?;
            if attr.attribute_type >= 0 {
                unit_type.costs.push(attr);
            }
        }
        unit_type.create_time = input.read_u16::<LE>()?;
        unit_type.create_at_building = read_opt_u16(&mut input)?;
        unit_type.create_button = input.read_i8()?;
        unit_type.rear_attack_modifier = input.read_f32::<LE>()?;
        unit_type.flank_attack_modifier = input.read_f32::<LE>()?;
        let _tribe_unit_type = input.read_u8()?;
        unit_type.hero_flag = input.read_u8()?;
        unit_type.garrison_sprite = {
            let n = input.read_i32::<LE>()?;
            if n < 0 {
                None
            } else {
                Some(n.try_into().unwrap())
            }
        };
        unit_type.volley_fire_amount = input.read_f32::<LE>()?;
        unit_type.max_attacks_in_volley = input.read_i8()?;
        unit_type.volley_spread = (input.read_f32::<LE>()?, input.read_f32::<LE>()?);
        unit_type.volley_start_spread_adjustment = input.read_f32::<LE>()?;
        unit_type.volley_missile = read_opt_u32(&mut input)?;
        unit_type.special_attack_sprite = read_opt_u32(&mut input)?;
        unit_type.special_attack_flag = input.read_i8()?;
        unit_type.displayed_pierce_armor = input.read_i16::<LE>()?;

        Ok(unit_type)
    }

    /// Write this unit type to an output stream.
    pub fn write_to(&self, mut output: impl Write, version: f32) -> Result<()> {
        self.superclass.write_to(&mut output, version)?;
        for i in 0..3 {
            match self.costs.get(i) {
                Some(cost) => cost.write_to(&mut output)?,
                None => AttributeCost {
                    attribute_type: -1,
                    amount: 0,
                    flag: 0,
                }
                .write_to(&mut output)?,
            }
        }
        output.write_u16::<LE>(self.create_time)?;
        output.write_u16::<LE>(self.create_at_building.map_into().unwrap_or(0xFFFF))?;
        output.write_i8(self.create_button)?;
        output.write_f32::<LE>(self.rear_attack_modifier)?;
        output.write_f32::<LE>(self.flank_attack_modifier)?;
        output.write_u8(0)?;
        output.write_u8(self.hero_flag)?;
        output.write_u32::<LE>(self.garrison_sprite.map_into().unwrap_or(0xFFFF_FFFF))?;
        output.write_f32::<LE>(self.volley_fire_amount)?;
        output.write_i8(self.max_attacks_in_volley)?;
        output.write_f32::<LE>(self.volley_spread.0)?;
        output.write_f32::<LE>(self.volley_spread.1)?;
        output.write_f32::<LE>(self.volley_start_spread_adjustment)?;
        output.write_u32::<LE>(self.volley_missile.map_into().unwrap_or(0xFFFF_FFFF))?;
        output.write_u32::<LE>(self.special_attack_sprite.map_into().unwrap_or(0xFFFF_FFFF))?;
        output.write_i8(self.special_attack_flag)?;
        output.write_i16::<LE>(self.displayed_pierce_armor)?;
        Ok(())
    }
}

/// A linked, or "Annex" building. These allow for buildings made up of multiple pieces
/// with different behaviour, like the Town Centre with some walkable tiles and some non-walkable
/// tiles.
#[derive(Debug, Default, Clone)]
pub struct LinkedBuilding {
    /// Unit type ID for this linked building.
    pub unit_id: UnitTypeID,
    /// X offset in tiles from the centre of the "owner" building.
    pub x_offset: f32,
    /// Y offset in tiles from the centre of the "owner" building.
    pub y_offset: f32,
}

impl LinkedBuilding {
    pub fn read_from(mut input: impl Read) -> Result<Self> {
        Ok(Self {
            unit_id: input.read_u16::<LE>()?.into(),
            x_offset: input.read_f32::<LE>()?,
            y_offset: input.read_f32::<LE>()?,
        })
    }
    pub fn write_to(&self, mut output: impl Write) -> Result<()> {
        output.write_u16::<LE>(self.unit_id.into())?;
        output.write_f32::<LE>(self.x_offset)?;
        output.write_f32::<LE>(self.y_offset)?;
        Ok(())
    }
    fn write_empty(mut output: impl Write) -> Result<()> {
        output.write_u16::<LE>(0xFFFF)?;
        output.write_f32::<LE>(0.0)?;
        output.write_f32::<LE>(0.0)?;
        Ok(())
    }
}

/// Unit type class for buildings.
#[derive(Debug, Default, Clone)]
pub struct BuildingUnitType {
    superclass: CombatUnitType,
    /// Sprite to use during construction.
    pub construction_sprite: Option<SpriteID>,
    /// Sprite to use when this building is finished and built on snow.
    pub snow_sprite: Option<SpriteID>,
    /// TODO document
    pub connect_flag: u8,
    /// TODO document
    pub facet: i16,
    /// Whether the building should be immediately destroyed on completion.
    pub destroy_on_build: bool,
    /// Unit to spawn at the build site on completion.
    pub on_build_make_unit: Option<UnitTypeID>,
    /// Change the underlying terrain to this terrain ID on completion.
    pub on_build_make_tile: Option<TerrainID>,
    /// TODO document
    pub on_build_make_overlay: i16,
    /// Research this tech on completion.
    pub on_build_make_tech: Option<TechID>,
    /// Whether this building…can burn?
    ///
    /// TODO document the details
    pub can_burn: bool,
    pub linked_buildings: ArrayVec<[LinkedBuilding; 4]>,
    pub construction_unit: Option<UnitTypeID>,
    pub transform_unit: Option<UnitTypeID>,
    pub transform_sound: Option<SoundID>,
    pub construction_sound: Option<SoundID>,
    pub garrison_type: i8,
    pub garrison_heal_rate: f32,
    pub garrison_repair_rate: f32,
    pub salvage_unit: Option<UnitTypeID>,
    pub salvage_attributes: ArrayVec<[i8; 6]>,
}

impl BuildingUnitType {
    /// Read this unit type from an input stream.
    pub fn read_from(mut input: impl Read, version: f32) -> Result<Self> {
        let mut unit_type = Self {
            superclass: CombatUnitType::read_from(&mut input, version)?,
            ..Default::default()
        };
        unit_type.construction_sprite = read_opt_u16(&mut input)?;
        unit_type.snow_sprite = if version < 11.53 {
            None
        } else {
            read_opt_u16(&mut input)?
        };
        unit_type.connect_flag = input.read_u8()?;
        unit_type.facet = input.read_i16::<LE>()?;
        unit_type.destroy_on_build = input.read_u8()? != 0;
        unit_type.on_build_make_unit = read_opt_u16(&mut input)?;
        unit_type.on_build_make_tile = read_opt_u16(&mut input)?;
        unit_type.on_build_make_overlay = input.read_i16::<LE>()?;
        unit_type.on_build_make_tech = read_opt_u16(&mut input)?;
        unit_type.can_burn = input.read_u8()? != 0;
        for _ in 0..unit_type.linked_buildings.capacity() {
            let link = LinkedBuilding::read_from(&mut input)?;
            if link.unit_id != 0xFFFF.into() {
                unit_type.linked_buildings.push(link);
            }
        }

        unit_type.construction_unit = read_opt_u16(&mut input)?;
        unit_type.transform_unit = read_opt_u16(&mut input)?;
        unit_type.transform_sound = read_opt_u16(&mut input)?;
        unit_type.construction_sound = read_opt_u16(&mut input)?;
        unit_type.garrison_type = input.read_i8()?;
        unit_type.garrison_heal_rate = input.read_f32::<LE>()?;
        unit_type.garrison_repair_rate = input.read_f32::<LE>()?;
        unit_type.salvage_unit = read_opt_u16(&mut input)?;
        for _ in 0..unit_type.salvage_attributes.capacity() {
            let attr = input.read_i8()?;
            unit_type.salvage_attributes.push(attr);
        }
        Ok(unit_type)
    }

    /// Write the unit type to an output stream.
    pub fn write_to(&self, mut output: impl Write, version: f32) -> Result<()> {
        self.superclass.write_to(&mut output, version)?;
        output.write_u16::<LE>(self.construction_sprite.map_into().unwrap_or(0xFFFF))?;
        if version >= 11.53 {
            output.write_u16::<LE>(self.snow_sprite.map_into().unwrap_or(0xFFFF))?;
        }
        output.write_u8(self.connect_flag)?;
        output.write_i16::<LE>(self.facet)?;
        output.write_u8(if self.destroy_on_build { 1 } else { 0 })?;
        output.write_u16::<LE>(self.on_build_make_unit.map_into().unwrap_or(0xFFFF))?;
        output.write_u16::<LE>(self.on_build_make_tile.map_into().unwrap_or(0xFFFF))?;
        output.write_i16::<LE>(self.on_build_make_overlay)?;
        output.write_u16::<LE>(self.on_build_make_tech.map_into().unwrap_or(0xFFFF))?;
        output.write_u8(if self.can_burn { 1 } else { 0 })?;
        for i in 0..self.linked_buildings.capacity() {
            match self.linked_buildings.get(i) {
                Some(link) => link.write_to(&mut output)?,
                None => LinkedBuilding::write_empty(&mut output)?,
            }
        }
        output.write_u16::<LE>(self.construction_unit.map_into().unwrap_or(0xFFFF))?;
        output.write_u16::<LE>(self.transform_unit.map_into().unwrap_or(0xFFFF))?;
        output.write_u16::<LE>(self.transform_sound.map_into().unwrap_or(0xFFFF))?;
        output.write_u16::<LE>(self.construction_sound.map_into().unwrap_or(0xFFFF))?;
        output.write_i8(self.garrison_type)?;
        output.write_f32::<LE>(self.garrison_heal_rate)?;
        output.write_f32::<LE>(self.garrison_repair_rate)?;
        output.write_u16::<LE>(self.salvage_unit.map_into().unwrap_or(0xFFFF))?;
        for attr in &self.salvage_attributes {
            output.write_i8(*attr)?;
        }
        Ok(())
    }
}

fn write_opt_string_key(mut output: impl Write, opt_key: &Option<StringKey>) -> Result<()> {
    output.write_i16::<LE>(if let Some(key) = opt_key {
        key.try_into()
            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?
    } else {
        -1
    })?;
    Ok(())
}