genie_scx/convert/mod.rs
1//! Automated scenario conversions.
2//!
3//! This module implements conversions between different scenario formats and game versions.
4mod aoc_to_wk;
5mod hd_to_wk;
6
7use crate::{Scenario, ScenarioObject};
8
9pub use aoc_to_wk::AoCToWK;
10pub use hd_to_wk::HDToWK;
11
12/// Error indicating scenario conversion failure.
13#[derive(Debug, thiserror::Error)]
14pub enum ConvertError {
15 /// The input scenario version is not supported by the converter.
16 #[error("invalid version")]
17 InvalidVersion,
18}
19
20/// Convert an AoC or HD Edition scenario file to a WololoKingdoms one.
21///
22/// It will auto-detect the version of the file, and output a WK compatible scenario.
23/// AoC scenarios will have their unit and terrain IDs switched around so they have the correct
24/// look in WK.
25/// HD Edition scenarios will have all the new unit and terrain IDs mapped to WK IDs.
26///
27/// ## Usage
28///
29/// ```rust,ignore
30/// use genie_scx::convert::AutoToWK;
31/// AutoToWK::default().convert(&mut scenario)?
32/// ```
33pub struct AutoToWK {}
34
35impl Default for AutoToWK {
36 fn default() -> Self {
37 AutoToWK {}
38 }
39}
40
41/// Check if a scenario object is likely a WololoKingdoms one.
42fn is_wk_object(object: &ScenarioObject) -> bool {
43 // Stormy Dog is the highest ID in AoC 1.0c.
44 const STORMY_DOG: i32 = 862;
45 object.id > STORMY_DOG
46}
47
48impl AutoToWK {
49 pub fn convert(&self, scen: &mut Scenario) -> Result<(), ConvertError> {
50 if scen.version().is_hd_edition() {
51 HDToWK::default().convert(scen)
52 } else if scen.version().is_aok() || scen.version().is_aoc() {
53 if scen.objects().any(is_wk_object) {
54 // No conversion necessary.
55 Ok(())
56 } else {
57 AoCToWK::default().convert(scen)
58 }
59 } else {
60 Err(ConvertError::InvalidVersion)
61 }
62 }
63}