Skip to main content

unbanished_formats/game/
work.rs

1//! Workplaces and production: quarries, herbalists, foresters, fishing, hunting, fields, orchards, pastures, livestock, recipes and professions.
2
3mod produce;
4pub use produce::{consume_produce_description, ConsumeProduceDescription, ProduceRecipe};
5
6use super::*;
7
8// +0x20; +0x28; +0x2c
9/// `QuarryDescription`: the pit the quarry digs, a picking mesh in model space whose vertices set
10/// the terrain, no lower than a floor that goes from `shallow` (when built) to `deep` (at the end of its
11/// production).
12///
13/// Original: `FUN_140055c80`.
14#[derive(Debug, Clone, PartialEq)]
15pub struct QuarryDescription {
16    /// The floor at the end of its production.
17    pub deep: f32,
18    /// The floor when built.
19    pub shallow: f32,
20    /// The picking mesh, in model space.
21    pub mesh: Option<String>,
22}
23
24/// Read a [`QuarryDescription`] from the bytes of its resource.
25pub fn quarry_description(data: &[u8]) -> Result<QuarryDescription> {
26    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
27        if h.type_id != type_id("QuarryDescription") {
28            return format_err(format!(
29                "expected QuarryDescription, found {:#010x}",
30                h.type_id
31            ));
32        }
33        r.block(|r, _| {
34            Ok(QuarryDescription {
35                deep: r.f32()?,
36                shallow: r.f32()?,
37                mesh: r.external_ref()?,
38            })
39        })
40    })?;
41    Ok(value)
42}
43
44// +0x20; +0x28; +0x30
45/// `HerbalistDescription`: the natural-resource templates a gatherer or herbalist harvests (count)
46/// and the stock-manager production group its limit is checked against (5 food, 6 herbs).
47///
48/// Original: `FUN_14004bc40`.
49#[derive(Debug, Clone, PartialEq, Eq)]
50pub struct HerbalistDescription {
51    /// The natural-resource templates it harvests.
52    pub resources: Vec<Option<String>>,
53    /// The stock-manager group its limit is checked against.
54    pub group: i32,
55}
56
57/// Read a [`HerbalistDescription`] from the bytes of its resource.
58pub fn herbalist_description(data: &[u8]) -> Result<HerbalistDescription> {
59    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
60        if h.type_id != type_id("HerbalistDescription") {
61            return format_err(format!(
62                "expected HerbalistDescription, found {:#010x}",
63                h.type_id
64            ));
65        }
66        r.block(|r, _| {
67            let n = r.i32()?;
68            let resources = (0..n.max(0))
69                .map(|_| r.external_ref())
70                .collect::<Result<Vec<_>>>()?;
71            Ok(HerbalistDescription {
72                resources,
73                group: r.i32()?,
74            })
75        })
76    })?;
77    Ok(value)
78}
79
80// +0x20; +0x28
81/// `ForesterDescription`: the tree template it plants and, from version 1, the stock-manager slot
82/// of its limit (logs, 0).
83///
84/// Original: `FUN_140124f50`.
85#[derive(Debug, Clone, PartialEq, Eq)]
86pub struct ForesterDescription {
87    /// The tree template it plants.
88    pub tree: Option<String>,
89    /// The stock-manager slot of its limit (version 1).
90    pub group: i32,
91}
92
93/// Read a [`ForesterDescription`] from the bytes of its resource.
94pub fn forester_description(data: &[u8]) -> Result<ForesterDescription> {
95    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
96        if h.type_id != type_id("ForesterDescription") {
97            return format_err(format!(
98                "expected ForesterDescription, found {:#010x}",
99                h.type_id
100            ));
101        }
102        r.block(|r, version| {
103            let tree = r.external_ref()?;
104            let group = if version > 0 { r.i32()? } else { 0 };
105            Ok(ForesterDescription { tree, group })
106        })
107    })?;
108    Ok(value)
109}
110
111// +0x20; +0x2c; +0x28
112/// `FishingDescription`: the catch's raw-material template, the cell types counted as fishing water
113/// and, from version 1, the stock-manager slot of its limit.
114///
115/// Original: `FUN_140124640`.
116#[derive(Debug, Clone, PartialEq, Eq)]
117pub struct FishingDescription {
118    /// The catch's raw-material template.
119    pub output: Option<String>,
120    /// The cell types counted as fishing water.
121    pub mask: u32,
122    /// The stock-manager slot of its limit (version 1).
123    pub group: i32,
124}
125
126/// Read a [`FishingDescription`] from the bytes of its resource.
127pub fn fishing_description(data: &[u8]) -> Result<FishingDescription> {
128    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
129        if h.type_id != type_id("FishingDescription") {
130            return format_err(format!(
131                "expected FishingDescription, found {:#010x}",
132                h.type_id
133            ));
134        }
135        r.block(|r, version| {
136            let output = r.external_ref()?;
137            let mask = r.u32()?;
138            let group = if version > 0 { r.i32()? } else { 0 };
139            Ok(FishingDescription {
140                output,
141                mask,
142                group,
143            })
144        })
145    })?;
146    Ok(value)
147}
148
149// +0x20; +0x24
150/// `HuntDescription` (versioned): the hunting time a kill needs (× 300 s of a full lodge's work)
151/// and, from version 1, the stock slot of its limit.
152///
153/// Original: `FUN_140127e30`.
154#[derive(Debug, Clone, Copy, PartialEq)]
155pub struct HuntDescription {
156    /// The hunting time a kill needs, in units of 300 seconds of a full lodge's work.
157    pub time: f32,
158    /// The stock slot of its limit (version 1).
159    pub group: i32,
160}
161
162/// Read a [`HuntDescription`] from the bytes of its resource.
163pub fn hunt_description(data: &[u8]) -> Result<HuntDescription> {
164    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
165        if h.type_id != type_id("HuntDescription") {
166            return format_err(format!(
167                "expected HuntDescription, found {:#010x}",
168                h.type_id
169            ));
170        }
171        r.block(|r, version| {
172            let time = r.f32()?;
173            let group = if version > 0 { r.i32()? } else { 0 };
174            Ok(HuntDescription { time, group })
175        })
176    })?;
177    Ok(value)
178}
179
180// +0x24; +0x20
181/// `CropFieldDescription` (versioned): a tending visit's growth and, from version 1, the stock
182/// slot.
183///
184/// Original: `FUN_140120740`.
185pub fn crop_field_description(data: &[u8]) -> Result<(f32, i32)> {
186    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
187        if h.type_id != type_id("CropFieldDescription") {
188            return format_err(format!(
189                "expected CropFieldDescription, found {:#010x}",
190                h.type_id
191            ));
192        }
193        r.block(|r, version| {
194            let tend = r.f32()?;
195            let group = if version > 0 { r.i32()? } else { 0 };
196            Ok((tend, group))
197        })
198    })?;
199    Ok(value)
200}
201
202// +0x24; +0x28; +0x2c; +0x20
203/// `OrchardDescription` (versioned): tree spacing, a tending visit's ripening and, from version 1,
204/// the stock slot.
205///
206/// Original: `FUN_140052c60`.
207pub fn orchard_description(data: &[u8]) -> Result<((i32, i32), f32, i32)> {
208    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
209        if h.type_id != type_id("OrchardDescription") {
210            return format_err(format!(
211                "expected OrchardDescription, found {:#010x}",
212                h.type_id
213            ));
214        }
215        r.block(|r, version| {
216            let spacing = (r.i32()?, r.i32()?);
217            let tend = r.f32()?;
218            let group = if version > 0 { r.i32()? } else { 0 };
219            Ok((spacing, tend, group))
220        })
221    })?;
222    Ok(value)
223}
224
225// +0x20. The default limit of 5 comes from the factory FUN_140037100.
226/// A `PastureDescription`. From version 1, it has the stock slot of the limit of its products (5
227/// by default).
228///
229/// Original: `FUN_14012d6f0`.
230pub fn pasture_description(data: &[u8]) -> Result<i32> {
231    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
232        if h.type_id != type_id("PastureDescription") {
233            return format_err(format!(
234                "expected PastureDescription, found {:#010x}",
235                h.type_id
236            ));
237        }
238        r.block(|r, version| if version > 0 { r.i32() } else { Ok(5) })
239    })?;
240    Ok(value)
241}
242
243// +0x20; +0x2c; +0x28
244/// `LivestockDescription` (versioned): the raw material a pasture gathers from the animal (none for
245/// cattle), every × 300 s, and from version 1 the stock slot.
246///
247/// Original: `FUN_140129430`.
248#[derive(Debug, Clone, Default, PartialEq)]
249pub struct LivestockDescription {
250    /// The raw material a pasture gathers from the animal (none for cattle).
251    pub product: Option<String>,
252    /// The time between products, in units of 300 seconds.
253    pub interval: f32,
254    /// The stock slot (version 1).
255    pub group: i32,
256}
257
258/// Read a [`LivestockDescription`] from the bytes of its resource.
259pub fn livestock_description(data: &[u8]) -> Result<LivestockDescription> {
260    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
261        if h.type_id != type_id("LivestockDescription") {
262            return format_err(format!(
263                "expected LivestockDescription, found {:#010x}",
264                h.type_id
265            ));
266        }
267        r.block(|r, version| {
268            let product = r.external_ref()?;
269            let interval = r.f32()?;
270            let group = if version > 0 { r.i32()? } else { 0 };
271            Ok(LivestockDescription {
272                product,
273                interval,
274                group,
275            })
276        })
277    })?;
278    Ok(value)
279}
280
281// +0x20; +0x28
282/// `WorkPlaceDescription`: the profession its workers take (none for storage barns and houses) and
283/// a second reference.
284///
285/// Original: `FUN_1401348f0`.
286#[derive(Debug, Clone, Default)]
287pub struct WorkPlaceDescription {
288    /// The profession its workers take (none for storage barns and houses).
289    pub profession: Option<String>,
290    /// Not identified yet.
291    pub reference_28: Option<String>,
292}
293
294/// Read a [`WorkPlaceDescription`] from the bytes of its resource.
295pub fn workplace_description(data: &[u8]) -> Result<WorkPlaceDescription> {
296    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
297        if h.type_id != type_id("WorkPlaceDescription") {
298            return format_err(format!(
299                "expected WorkPlaceDescription, found {:#010x}",
300                h.type_id
301            ));
302        }
303        r.block(|r, _| {
304            Ok(WorkPlaceDescription {
305                profession: r.external_ref()?,
306                reference_28: r.external_ref()?,
307            })
308        })
309    })?;
310    Ok(value)
311}
312
313/// `WorkDescription` (`Template\X.rsc:work`).
314///
315/// Original: `FUN_140133d80`.
316#[derive(Debug, Clone, Default)]
317pub struct WorkDescription {
318    // +0x24
319    /// Most workers.
320    ///
321    /// Original: `FUN_140133a90`.
322    pub max_workers: i32,
323    /// The fewest workers the player may set.
324    pub value_28: i32,
325    /// The most workers the player may set.
326    pub value_2c: i32,
327    // +0x34; +0x30
328    /// Scale the most workers by the footprint's area over `area` cells.
329    pub per_area: bool,
330    /// The area, in cells, that `per_area` divides by.
331    pub area: i32,
332    /// Whether the player may change the most workers in the building's window.
333    pub flag_20: bool,
334}
335
336/// Read a [`WorkDescription`] from the bytes of its resource.
337pub fn work_description(data: &[u8]) -> Result<WorkDescription> {
338    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
339        if h.type_id != type_id("WorkDescription") {
340            return format_err(format!(
341                "expected WorkDescription, found {:#010x}",
342                h.type_id
343            ));
344        }
345        r.block(|r, _| {
346            Ok(WorkDescription {
347                max_workers: r.i32()?,
348                value_28: r.i32()?,
349                value_2c: r.i32()?,
350                per_area: r.bool()?,
351                area: r.i32()?,
352                flag_20: r.bool()?,
353            })
354        })
355    })?;
356    Ok(value)
357}
358
359/// `Profession`: a job citizens can be assigned to.
360///
361/// Original: `FUN_1400ae3e0`.
362#[derive(Debug, Clone)]
363pub struct Profession {
364    // +0x20; +0x28
365    /// The string table.
366    pub string_table: Option<String>,
367    /// The icon's sprite sheet.
368    pub sprite_sheet: Option<String>,
369    // +0x30
370    /// Name key, also the icon's sprite name (`ProfessionBuilder`).
371    pub name: String,
372    // +0x38, +0x40
373    /// The tooltip's key.
374    pub tooltip: String,
375    /// The death-message key of an accident at its work.
376    pub death: String,
377    /// The sort priority in the professions list.
378    pub value_48: i32,
379    /// Works anywhere (laborers and builders).
380    pub flag_4c: bool,
381    /// With a workplace, the citizen does not catch diseases (the doctor).
382    pub flag_4d: bool,
383    /// The accident odds' years.
384    pub value_54: f32,
385    /// The accident odds' factor.
386    pub value_58: f32,
387    /// How much a worker carries (the laborer's is what home stocking carries at least).
388    pub value_50: i32,
389    /// Whether it is in the professions list (*inferred*).
390    pub flag_4e: bool,
391}
392
393/// Read a [`Profession`] from the bytes of its resource.
394pub fn profession(data: &[u8]) -> Result<Profession> {
395    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
396        if h.type_id != type_id("Profession") {
397            return format_err(format!("expected Profession, found {:#010x}", h.type_id));
398        }
399        r.block(|r, _| {
400            Ok(Profession {
401                string_table: r.external_ref()?,
402                sprite_sheet: r.external_ref()?,
403                name: r.string()?,
404                tooltip: r.string()?,
405                death: r.string()?,
406                value_48: r.i32()?,
407                flag_4c: r.bool()?,
408                flag_4d: r.bool()?,
409                value_54: r.f32()?,
410                value_58: r.f32()?,
411                value_50: r.i32()?,
412                flag_4e: r.bool()?,
413            })
414        })
415    })?;
416    Ok(value)
417}
418
419// +0x28; +0x20
420/// RadiusDescription: radius, display reference.
421///
422/// Original: `FUN_140055e90`.
423pub fn radius_description(data: &[u8]) -> Result<(i32, Option<String>)> {
424    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
425        if h.type_id != type_id("RadiusDescription") {
426            return format_err("expected RadiusDescription");
427        }
428        r.block(|r, _| Ok((r.i32()?, r.external_ref()?)))
429    })?;
430    Ok(value)
431}