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
use crate::civ::CivilizationID;
use crate::unit_type::UnitTypeID;
use arrayvec::ArrayVec;
use byteorder::{ReadBytesExt, WriteBytesExt, LE};
use genie_support::{read_opt_u32, TechID};
use std::convert::{TryFrom, TryInto};
use std::io::{self, Read, Result, Write};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TechTreeStatus {
    None,
    /// This building/unit/technology is available to the player.
    AvailablePlayer,
    /// This building/unit/technology is not available to the player.
    NotAvailablePlayer,
    /// Researching or constructing or creating.
    Researching,
    /// Researched or built.
    ResearchedCompleted,
    /// This building/unit/technology is available to the player if someone on their team is this
    /// civilization.
    AvailableTeam {
        civilization_id: CivilizationID,
    },
}

impl std::default::Default for TechTreeStatus {
    fn default() -> Self {
        Self::None
    }
}

#[derive(Debug, Clone, Copy, thiserror::Error)]
#[error("invalid tech tree node status {} (must be 1-5)", .0)]
pub struct ParseTechTreeStatusError(u8);

impl TryFrom<u8> for TechTreeStatus {
    type Error = ParseTechTreeStatusError;

    fn try_from(n: u8) -> std::result::Result<Self, Self::Error> {
        match n {
            1 => Ok(Self::None),
            2 => Ok(Self::AvailablePlayer),
            3 => Ok(Self::NotAvailablePlayer),
            4 => Ok(Self::Researching),
            5 => Ok(Self::ResearchedCompleted),
            10..=255 => Ok(Self::AvailableTeam {
                civilization_id: (n - 10).into(),
            }),
            n => Err(ParseTechTreeStatusError(n as u8)),
        }
    }
}

impl From<TechTreeStatus> for u8 {
    fn from(status: TechTreeStatus) -> Self {
        match status {
            TechTreeStatus::None => 1,
            TechTreeStatus::AvailablePlayer => 2,
            TechTreeStatus::NotAvailablePlayer => 3,
            TechTreeStatus::Researching => 4,
            TechTreeStatus::ResearchedCompleted => 5,
            TechTreeStatus::AvailableTeam { civilization_id } => u8::from(civilization_id) + 10,
        }
    }
}

/// Kinds of tech tree nodes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TechTreeType {
    None = 0,
    Age = 1,
    Unit = 2,
    UnitUpgrade = 3,
    Research = 4,
    BuildingTech = 5,
    BuildingNonTech = 6,
    UniqueUnit = 7,
}

impl std::default::Default for TechTreeType {
    fn default() -> Self {
        Self::None
    }
}

#[derive(Debug, Clone, Copy, thiserror::Error)]
#[error("invalid tech tree node type {} (must be 0-7)", .0)]
pub struct ParseTechTreeTypeError(i32);

impl TryFrom<i32> for TechTreeType {
    type Error = ParseTechTreeTypeError;

    fn try_from(n: i32) -> std::result::Result<Self, Self::Error> {
        match n {
            0 => Ok(Self::None),
            1 => Ok(Self::Age),
            2 => Ok(Self::Unit),
            3 => Ok(Self::UnitUpgrade),
            4 => Ok(Self::Research),
            5 => Ok(Self::BuildingTech),
            6 => Ok(Self::BuildingNonTech),
            7 => Ok(Self::UniqueUnit),
            n => Err(ParseTechTreeTypeError(n)),
        }
    }
}

impl From<TechTreeType> for u32 {
    fn from(ty: TechTreeType) -> Self {
        ty as u32
    }
}

impl From<TechTreeType> for i32 {
    fn from(ty: TechTreeType) -> Self {
        ty as i32
    }
}

#[derive(Debug, Default, Clone)]
pub struct TechTree {
    pub ages: Vec<TechTreeAge>,
    pub buildings: Vec<TechTreeBuilding>,
    pub units: Vec<TechTreeUnit>,
    pub techs: Vec<TechTreeTech>,
    num_groups: i32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TechTreeDependency {
    /// A dependency on an age being researched.
    ///
    /// This can typically only be 0-4, for Dark Age through Post-Imperial Age, or up to 5 in mods
    /// that add a new age. However the dat file format has space for 32 bits, and some data files
    /// in the wild contain incorrect data with much higher "age" IDs, so we have to follow suit.
    Age(i32),
    /// A dependency on a building.
    Building(UnitTypeID),
    /// A dependency on a unit.
    Unit(UnitTypeID),
    /// A dependency on a research/tech.
    Research(TechID),
}

impl TechTreeDependency {
    fn dependency_type(&self) -> TechTreeDependencyType {
        match self {
            Self::Age(_) => TechTreeDependencyType::Age,
            Self::Building(_) => TechTreeDependencyType::Building,
            Self::Unit(_) => TechTreeDependencyType::Unit,
            Self::Research(_) => TechTreeDependencyType::Research,
        }
    }

    fn raw_id(&self) -> i32 {
        match *self {
            Self::Age(id) => id.into(),
            Self::Building(id) => id.into(),
            Self::Unit(id) => id.into(),
            Self::Research(id) => {
                let id: u16 = id.into();
                id as i32
            }
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum TechTreeDependencyType {
    Age = 0,
    Building = 1,
    Unit = 2,
    Research = 3,
}

#[derive(Debug, Clone, Copy, thiserror::Error)]
#[error("invalid tech tree dependency type {} (must be 0-3)", .0)]
pub struct ParseTechTreeDependencyTypeError(i32);

impl TryFrom<i32> for TechTreeDependencyType {
    type Error = ParseTechTreeDependencyTypeError;

    fn try_from(n: i32) -> std::result::Result<Self, Self::Error> {
        match n {
            0 => Ok(Self::Age),
            1 => Ok(Self::Building),
            2 => Ok(Self::Unit),
            3 => Ok(Self::Research),
            n => Err(ParseTechTreeDependencyTypeError(n)),
        }
    }
}

impl Into<i32> for TechTreeDependencyType {
    fn into(self) -> i32 {
        self as i32
    }
}

#[derive(Debug, Default, Clone)]
pub struct TechTreeDependencies(ArrayVec<[TechTreeDependency; 10]>);

#[derive(Debug, Default, Clone)]
pub struct TechTreeAge {
    age_id: i32,
    status: TechTreeStatus,
    node_type: TechTreeType,
    /// The buildings that become available in this age.
    pub dependent_buildings: Vec<UnitTypeID>,
    /// The units that become available in this age.
    pub dependent_units: Vec<UnitTypeID>,
    /// The techs that become available in this age.
    pub dependent_techs: Vec<TechID>,
    prerequisites: TechTreeDependencies,
    building_levels: u8,
    buildings_per_zone: [u8; 10],
    group_length_per_zone: [u8; 10],
    max_age_length: u8,
}

#[derive(Debug, Default, Clone)]
pub struct TechTreeBuilding {
    building_id: UnitTypeID,
    status: TechTreeStatus,
    node_type: TechTreeType,
    /// The tech ID that makes this building available. `None` if the building is available without
    /// requiring any techs.
    pub depends_tech_id: Option<TechID>,
    /// The buildings that become available by building this building.
    pub dependent_buildings: Vec<UnitTypeID>,
    /// The units that become available by building this building.
    pub dependent_units: Vec<UnitTypeID>,
    /// The techs that become available by building this building.
    pub dependent_techs: Vec<TechID>,
    prerequisites: TechTreeDependencies,
    /// ?
    level_no: u8,
    /// Total units and techs at this building by age, including ones that require research to
    /// unlock.
    total_children_by_age: [u8; 5],
    /// Initial units and techs at this building by age, excluding ones that require research to
    /// unlock.
    initial_children_by_age: [u8; 5],
}

#[derive(Debug, Default, Clone)]
pub struct TechTreeUnit {
    unit_id: UnitTypeID,
    status: TechTreeStatus,
    node_type: TechTreeType,
    depends_tech_id: Option<TechID>,
    building: UnitTypeID,
    requires_tech_id: Option<TechID>,
    dependent_units: Vec<UnitTypeID>,
    prerequisites: TechTreeDependencies,
    group_id: i32,
    level_no: i32,
}

#[derive(Debug, Default, Clone)]
pub struct TechTreeTech {
    tech_id: TechID,
    status: TechTreeStatus,
    node_type: TechTreeType,
    building: UnitTypeID,
    dependent_buildings: Vec<UnitTypeID>,
    dependent_units: Vec<UnitTypeID>,
    dependent_techs: Vec<TechID>,
    prerequisites: TechTreeDependencies,
    group_id: i32,
    level_no: i32,
}

impl TechTree {
    pub fn read_from(mut input: impl Read) -> Result<Self> {
        let num_ages = input.read_u8()?;
        let num_buildings = input.read_u8()?;
        let num_units = input.read_u8()?;
        let num_techs = input.read_u8()?;
        let num_groups = input.read_i32::<LE>()?;

        let mut ages = vec![];
        for _ in 0..num_ages {
            ages.push(TechTreeAge::read_from(&mut input)?);
        }

        let mut buildings = vec![];
        for _ in 0..num_buildings {
            buildings.push(TechTreeBuilding::read_from(&mut input)?);
        }

        let mut units = vec![];
        for _ in 0..num_units {
            units.push(TechTreeUnit::read_from(&mut input)?);
        }

        let mut techs = vec![];
        for _ in 0..num_techs {
            techs.push(TechTreeTech::read_from(&mut input)?);
        }

        Ok(Self {
            ages,
            buildings,
            units,
            techs,
            num_groups,
        })
    }

    pub fn write_to(&self, mut output: impl Write) -> Result<()> {
        output.write_u8(self.ages.len() as u8)?;
        output.write_u8(self.buildings.len() as u8)?;
        output.write_u8(self.units.len() as u8)?;
        output.write_u8(self.techs.len() as u8)?;
        output.write_i32::<LE>(self.num_groups)?;

        for age in &self.ages {
            age.write_to(&mut output)?;
        }
        for building in &self.buildings {
            building.write_to(&mut output)?;
        }
        for unit in &self.units {
            unit.write_to(&mut output)?;
        }
        for tech in &self.techs {
            tech.write_to(&mut output)?;
        }

        Ok(())
    }
}

impl TechTreeDependencies {
    pub fn read_from<R: Read>(input: &mut R) -> Result<Self> {
        let mut deps = Self::default();
        let num = input.read_u8()?;
        let _padding = input.read_u8()?;
        let _padding = input.read_u8()?;
        let _padding = input.read_u8()?;

        let mut ids = [-1i32; 10];
        for id in ids.iter_mut() {
            *id = input.read_i32::<LE>()?;
        }
        let mut types = [-1i32; 10];
        for ty in types.iter_mut() {
            *ty = input.read_i32::<LE>()?;
        }

        for (&id, &ty) in ids.iter().zip(types.iter()).take(num as usize) {
            let dep_type: TechTreeDependencyType = ty.try_into().map_err(invalid_data)?;
            deps.0.push(match dep_type {
                TechTreeDependencyType::Age => {
                    TechTreeDependency::Age(id.try_into().map_err(invalid_data)?)
                }
                TechTreeDependencyType::Building => {
                    TechTreeDependency::Building(id.try_into().map_err(invalid_data)?)
                }
                TechTreeDependencyType::Unit => {
                    TechTreeDependency::Unit(id.try_into().map_err(invalid_data)?)
                }
                TechTreeDependencyType::Research => {
                    TechTreeDependency::Research(id.try_into().map_err(invalid_data)?)
                }
            });
        }

        Ok(deps)
    }

    pub fn write_to<W: Write>(&self, output: &mut W) -> Result<()> {
        assert!(self.len() <= 10);
        output.write_u8(self.len() as u8)?;
        output.write_all(&[0, 0, 0])?;
        for i in 0..10 {
            output.write_i32::<LE>(self.0.get(i).map(TechTreeDependency::raw_id).unwrap_or(0))?;
        }
        for i in 0..10 {
            output.write_i32::<LE>(
                self.0
                    .get(i)
                    .map(TechTreeDependency::dependency_type)
                    .map(Into::into)
                    .unwrap_or(0),
            )?;
        }
        Ok(())
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    pub fn iter(&self) -> impl Iterator<Item = &TechTreeDependency> {
        self.0.iter()
    }
}

/// Read a list of dependent "Thing" IDs for a tech tree node entry. The "Thing"
/// may be unit, building, tech IDs.
fn read_dependents<R, T>(input: &mut R) -> Result<Vec<T>>
where
    R: Read,
    T: TryFrom<i32>,
    // so `invalid_data` can convert it
    T::Error: std::error::Error + Send + Sync + 'static,
{
    let num = input.read_u8()?;
    let mut list = vec![];
    for _ in 0..num {
        list.push(input.read_i32::<LE>()?.try_into().map_err(invalid_data)?);
    }
    Ok(list)
}

impl TechTreeAge {
    pub fn read_from<R: Read>(input: &mut R) -> Result<Self> {
        let mut age = Self::default();
        age.age_id = input.read_i32::<LE>()?;
        age.status = input.read_u8()?.try_into().map_err(invalid_data)?;
        age.dependent_buildings = read_dependents(input)?;
        age.dependent_units = read_dependents(input)?;
        age.dependent_techs = read_dependents(input)?;
        age.prerequisites = TechTreeDependencies::read_from(input)?;
        age.building_levels = input.read_u8()?;
        assert!(age.building_levels <= 10);
        for building in age.buildings_per_zone.iter_mut() {
            *building = input.read_u8()?;
        }
        for group_length in age.group_length_per_zone.iter_mut() {
            *group_length = input.read_u8()?;
        }
        age.max_age_length = input.read_u8()?;
        age.node_type = input.read_i32::<LE>()?.try_into().map_err(invalid_data)?;
        assert_eq!(age.node_type, TechTreeType::Age);
        Ok(age)
    }

    pub fn write_to(&self, mut output: impl Write) -> Result<()> {
        output.write_i32::<LE>(self.age_id)?;
        output.write_u8(self.status.into())?;
        output.write_u8(self.dependent_buildings.len() as u8)?;
        for dependent in &self.dependent_buildings {
            output.write_u32::<LE>((*dependent).into())?;
        }
        output.write_u8(self.dependent_units.len() as u8)?;
        for dependent in &self.dependent_units {
            output.write_u32::<LE>((*dependent).into())?;
        }
        output.write_u8(self.dependent_techs.len() as u8)?;
        for dependent in &self.dependent_techs {
            output.write_u32::<LE>(u16::from(*dependent) as u32)?;
        }
        self.prerequisites.write_to(&mut output)?;
        output.write_u8(self.building_levels)?;
        output.write_all(&self.buildings_per_zone)?;
        output.write_all(&self.group_length_per_zone)?;
        output.write_u8(self.max_age_length)?;
        output.write_u32::<LE>(self.node_type.into())?;
        Ok(())
    }
}

impl TechTreeBuilding {
    pub fn read_from(mut input: impl Read) -> Result<Self> {
        let mut building = Self::default();
        building.building_id = input.read_i32::<LE>()?.try_into().map_err(invalid_data)?;
        building.status = input.read_u8()?.try_into().map_err(invalid_data)?;
        building.dependent_buildings = read_dependents(&mut input)?;
        building.dependent_units = read_dependents(&mut input)?;
        building.dependent_techs = read_dependents(&mut input)?;
        building.prerequisites = TechTreeDependencies::read_from(&mut input)?;
        building.level_no = input.read_u8()?;
        for children in building.total_children_by_age.iter_mut() {
            *children = input.read_u8()?;
        }
        for children in building.initial_children_by_age.iter_mut() {
            *children = input.read_u8()?;
        }
        building.node_type = input.read_i32::<LE>()?.try_into().map_err(invalid_data)?;
        building.depends_tech_id = read_opt_u32(&mut input)?;
        Ok(building)
    }

    pub fn write_to(&self, mut output: impl Write) -> Result<()> {
        output.write_u32::<LE>(self.building_id.into())?;
        output.write_u8(self.status.into())?;
        output.write_u8(self.dependent_buildings.len() as u8)?;
        for dependent in &self.dependent_buildings {
            output.write_u32::<LE>((*dependent).into())?;
        }
        output.write_u8(self.dependent_units.len() as u8)?;
        for dependent in &self.dependent_units {
            output.write_u32::<LE>((*dependent).into())?;
        }
        output.write_u8(self.dependent_techs.len() as u8)?;
        for dependent in &self.dependent_techs {
            output.write_u32::<LE>(u16::from(*dependent) as u32)?;
        }
        self.prerequisites.write_to(&mut output)?;
        output.write_u8(self.level_no)?;
        output.write_all(&self.total_children_by_age)?;
        output.write_all(&self.initial_children_by_age)?;
        output.write_u32::<LE>(self.node_type.into())?;
        output.write_u32::<LE>(
            self.depends_tech_id
                .map(|tech_id| u16::from(tech_id) as u32)
                .unwrap_or(0xFFFF_FFFF),
        )?;
        Ok(())
    }
}

impl TechTreeUnit {
    pub fn read_from(mut input: impl Read) -> Result<Self> {
        let mut unit = Self::default();
        unit.unit_id = input.read_i32::<LE>()?.try_into().map_err(invalid_data)?;
        unit.status = input.read_u8()?.try_into().map_err(invalid_data)?;
        unit.building = input.read_i32::<LE>()?.try_into().map_err(invalid_data)?;
        unit.prerequisites = TechTreeDependencies::read_from(&mut input)?;
        unit.group_id = input.read_i32::<LE>()?;
        unit.dependent_units = read_dependents(&mut input)?;
        unit.level_no = input.read_i32::<LE>()?;
        unit.requires_tech_id = read_opt_u32(&mut input)?;
        unit.node_type = input.read_i32::<LE>()?.try_into().map_err(invalid_data)?;
        unit.depends_tech_id = read_opt_u32(&mut input)?;
        Ok(unit)
    }

    pub fn write_to(&self, mut output: impl Write) -> Result<()> {
        output.write_u32::<LE>(u16::from(self.unit_id).into())?;
        output.write_u8(self.status.into())?;
        output.write_u32::<LE>(self.building.into())?;
        self.prerequisites.write_to(&mut output)?;
        output.write_i32::<LE>(self.group_id)?;
        output.write_u8(self.dependent_units.len() as u8)?;
        for dependent in &self.dependent_units {
            output.write_u32::<LE>((*dependent).into())?;
        }
        output.write_i32::<LE>(self.level_no)?;
        output.write_u32::<LE>(
            self.requires_tech_id
                .map(|tech_id| u16::from(tech_id) as u32)
                .unwrap_or(0xFFFF_FFFF),
        )?;
        output.write_u32::<LE>(self.node_type.into())?;
        output.write_u32::<LE>(
            self.depends_tech_id
                .map(|tech_id| u16::from(tech_id) as u32)
                .unwrap_or(0xFFFF_FFFF),
        )?;
        Ok(())
    }
}

impl TechTreeTech {
    pub fn read_from<R: Read>(input: &mut R) -> Result<Self> {
        let mut tech = Self::default();
        tech.tech_id = input.read_i32::<LE>()?.try_into().map_err(invalid_data)?;
        tech.status = input.read_u8()?.try_into().map_err(invalid_data)?;
        tech.building = input.read_i32::<LE>()?.try_into().map_err(invalid_data)?;
        tech.dependent_buildings = read_dependents(input)?;
        tech.dependent_units = read_dependents(input)?;
        tech.dependent_techs = read_dependents(input)?;
        tech.prerequisites = TechTreeDependencies::read_from(input)?;
        tech.group_id = input.read_i32::<LE>()?;
        tech.level_no = input.read_i32::<LE>()?;
        tech.node_type = input.read_i32::<LE>()?.try_into().map_err(invalid_data)?;
        Ok(tech)
    }

    pub fn write_to(&self, mut output: impl Write) -> Result<()> {
        output.write_u32::<LE>(u16::from(self.tech_id).into())?;
        output.write_u8(self.status.into())?;
        output.write_u32::<LE>(self.building.into())?;
        output.write_u8(self.dependent_buildings.len() as u8)?;
        for dependent in &self.dependent_buildings {
            output.write_u32::<LE>((*dependent).into())?;
        }
        output.write_u8(self.dependent_units.len() as u8)?;
        for dependent in &self.dependent_units {
            output.write_u32::<LE>((*dependent).into())?;
        }
        output.write_u8(self.dependent_techs.len() as u8)?;
        for dependent in &self.dependent_techs {
            output.write_u32::<LE>(u16::from(*dependent) as u32)?;
        }
        self.prerequisites.write_to(&mut output)?;
        output.write_i32::<LE>(self.group_id)?;
        output.write_i32::<LE>(self.level_no)?;
        output.write_u32::<LE>(self.node_type.into())?;
        Ok(())
    }
}

fn invalid_data<E: std::error::Error + Sized + Send + Sync + 'static>(err: E) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidData, err)
}