Skip to main content

unbanished_formats/scenario/
tutorial.rs

1//! Tutorials: their steps and the parts (text, images, goals) a step shows.
2
3use super::*;
4
5// +0x28; +0x20; +0x38
6/// `Tutorial`: its steps, its window (a UIDescription template, opened at the right edge), the
7/// scenario to start when it is done (none below 0).
8///
9/// Original: `FUN_1400e6240`.
10#[derive(Debug, Clone, Default, PartialEq)]
11pub struct Tutorial {
12    /// The steps.
13    pub steps: Vec<Option<String>>,
14    /// The window (a UIDescription template, opened at the right edge).
15    pub window: Option<String>,
16    /// The scenario to start when it is done (none below 0).
17    pub next: i32,
18}
19
20// +0x20; +0x28; +0x30; +0x38; +0x40; +0x50; +0x60
21/// `TutorialStep`: a checkpoint (a loaded game resumes at the latest one), the string table, the
22/// texts before and after, images, setups, goals.
23///
24/// Original: `FUN_1400e69f0`, `FUN_1400e7280`.
25#[derive(Debug, Clone, Default, PartialEq)]
26pub struct TutorialStep {
27    /// Whether this is a checkpoint (a loaded game resumes at the latest one).
28    pub checkpoint: bool,
29    /// The string table.
30    pub strings: Option<String>,
31    /// The text before.
32    pub pre: String,
33    /// The text after.
34    pub post: String,
35    /// The images.
36    pub images: Vec<Option<String>>,
37    /// The set-ups.
38    pub setups: Vec<Option<String>>,
39    /// The goals.
40    pub goals: Vec<Option<String>>,
41}
42
43/// One part of a tutorial: the tutorial itself, a step, or a set-up dialog.
44#[derive(Debug, Clone, PartialEq)]
45pub enum TutorialPart {
46    /// The tutorial.
47    Tutorial(Tutorial),
48    /// A step.
49    Step(TutorialStep),
50    // +0x20; +0x30; +0x40; +0x50; +0x48; +0x58; +0x70
51    /// A set-up that limits the interface and points at an element.
52    SetupDialog {
53        /// The elements left usable.
54        a: Vec<String>,
55        /// More elements left usable.
56        b: Vec<String>,
57        /// The element to point at.
58        s40: String,
59        /// The highlight dialog.
60        r50: Option<String>,
61        /// The angle of the pointer.
62        i48: i32,
63        /// A ComponentDescription.
64        component: String,
65        /// Not identified yet.
66        i70: i32,
67    },
68    /// Moves the camera to two cells.
69    SetupCamera([i32; 4]),
70    /// Sets the game speed.
71    SetupGameSpeed {
72        /// The speed.
73        speed: i32,
74        /// Whether it unpauses the game.
75        b24: bool,
76    },
77    /// Limits what the player may place, and where.
78    SetupRestrictPlayer {
79        /// A rectangle of cells, then one more value.
80        values: [i32; 5],
81        /// The first template.
82        s38: String,
83        /// The second template.
84        s50: String,
85    },
86    /// An element was pressed.
87    GoalButton {
88        /// The element's path.
89        s20: String,
90        /// Not identified yet.
91        b28: bool,
92    },
93    /// A count reached a number.
94    GoalCount {
95        /// What is counted.
96        e20: i32,
97        /// The number.
98        i24: i32,
99        /// The template.
100        s30: String,
101        /// Whether the count must be at least the number (else exactly).
102        b28: bool,
103        /// The spinner's path, for kind 9.
104        s48: String,
105    },
106    /// A dialog is open.
107    GoalDialog {
108        /// The dialog.
109        s20: String,
110        /// The template of the entity it is open on.
111        component: String,
112    },
113    /// The game speed is this value.
114    GoalGameSpeed(i32),
115    // +0x28; +0x2c; +0x20
116    /// A TutorialImage and its classes.
117    Image {
118        /// The column.
119        i28: i32,
120        /// The row.
121        i2c: i32,
122        /// The element.
123        r20: Option<String>,
124        /// What the class adds.
125        extra: ImageExtra,
126    },
127}
128
129/// What a tutorial image adds: a key name, an input key, or a toolbar icon.
130#[derive(Debug, Clone, PartialEq)]
131pub enum ImageExtra {
132    /// Nothing.
133    None,
134    /// A toolbar item's key.
135    HotKey(String),
136    /// A key-map action's key.
137    InputKey(i32),
138    /// A toolbar item's icon.
139    ToolbarIcon(String),
140}
141
142/// Read a [`TutorialPart`] from the bytes of its resource.
143pub fn tutorial_part(data: &[u8]) -> Result<TutorialPart> {
144    let (_, v) = crs::parse(data, |r, h| {
145        let t = h.type_id;
146        let is = |n: &str| t == type_id(n);
147        // A base class writes its own block first: TutorialGoal's is empty; TutorialImage's
148        // holds the image.
149        let goal = [
150            "TutorialGoalButton",
151            "TutorialGoalCount",
152            "TutorialGoalDialog",
153            "TutorialGoalGameSpeed",
154        ]
155        .iter()
156        .any(|n| is(n));
157        let image = [
158            "TutorialImageHotKey",
159            "TutorialImageInputKey",
160            "TutorialImageToolbarIcon",
161        ]
162        .iter()
163        .any(|n| is(n));
164        if goal {
165            r.block(|_, _| Ok(()))?;
166        }
167        let base = if image {
168            Some(r.block(|r, _| Ok((r.i32()?, r.i32()?, r.external_ref()?)))?)
169        } else {
170            None
171        };
172        r.block(|r, _| {
173            Ok(if is("Tutorial") {
174                let steps = refs(r)?;
175                TutorialPart::Tutorial(Tutorial {
176                    steps,
177                    window: r.external_ref()?,
178                    next: r.i32()?,
179                })
180            } else if is("TutorialStep") {
181                TutorialPart::Step(TutorialStep {
182                    checkpoint: r.bool()?,
183                    strings: r.external_ref()?,
184                    pre: r.string()?,
185                    post: r.string()?,
186                    images: refs(r)?,
187                    setups: refs(r)?,
188                    goals: refs(r)?,
189                })
190            } else if is("TutorialSetupDialog") {
191                let a = (0..r.count()?).map(|_| r.string()).collect::<Result<_>>()?;
192                let b = (0..r.count()?).map(|_| r.string()).collect::<Result<_>>()?;
193                TutorialPart::SetupDialog {
194                    a,
195                    b,
196                    s40: r.string()?,
197                    r50: r.external_ref()?,
198                    i48: r.i32()?,
199                    component: r.string()?,
200                    i70: r.i32()?,
201                }
202            } else if is("TutorialSetupCamera") {
203                TutorialPart::SetupCamera([r.i32()?, r.i32()?, r.i32()?, r.i32()?])
204            } else if is("TutorialSetupGameSpeed") {
205                TutorialPart::SetupGameSpeed {
206                    speed: r.i32()?,
207                    b24: r.bool()?,
208                }
209            } else if is("TutorialSetupRestrictPlayer") {
210                TutorialPart::SetupRestrictPlayer {
211                    values: [r.i32()?, r.i32()?, r.i32()?, r.i32()?, r.i32()?],
212                    s38: r.string()?,
213                    s50: r.string()?,
214                }
215            } else if is("TutorialGoalButton") {
216                TutorialPart::GoalButton {
217                    s20: r.string()?,
218                    b28: r.bool()?,
219                }
220            } else if is("TutorialGoalCount") {
221                TutorialPart::GoalCount {
222                    e20: r.i32()?,
223                    i24: r.i32()?,
224                    s30: r.string()?,
225                    b28: r.bool()?,
226                    s48: r.string()?,
227                }
228            } else if is("TutorialGoalDialog") {
229                TutorialPart::GoalDialog {
230                    s20: r.string()?,
231                    component: r.string()?,
232                }
233            } else if is("TutorialGoalGameSpeed") {
234                TutorialPart::GoalGameSpeed(r.i32()?)
235            } else if is("TutorialImage")
236                || is("TutorialImageHotKey")
237                || is("TutorialImageInputKey")
238                || is("TutorialImageToolbarIcon")
239            {
240                let (i28, i2c, r20) = match base.clone() {
241                    Some(b) => b,
242                    None => (r.i32()?, r.i32()?, r.external_ref()?),
243                };
244                let extra = if is("TutorialImageHotKey") {
245                    ImageExtra::HotKey(r.string()?)
246                } else if is("TutorialImageInputKey") {
247                    ImageExtra::InputKey(r.i32()?)
248                } else if is("TutorialImageToolbarIcon") {
249                    ImageExtra::ToolbarIcon(r.string()?)
250                } else {
251                    ImageExtra::None
252                };
253                TutorialPart::Image {
254                    i28,
255                    i2c,
256                    r20,
257                    extra,
258                }
259            } else {
260                return format_err(format!("not a tutorial part: {t:#010x}"));
261            })
262        })
263    })?;
264    Ok(v)
265}