unbanished_formats/game/work/produce.rs
1//! Consume-produce descriptions: the recipes a workplace turns inputs into outputs with.
2
3use super::*;
4
5/// One production recipe of a [`ConsumeProduceDescription`]: what a workplace uses and what it
6/// makes.
7///
8/// Original: the 0x28-byte record read by `FUN_140033050`.
9#[derive(Debug, Clone, Default)]
10pub struct ProduceRecipe {
11 // +0x00/+0x08
12 /// Input raw-material templates and counts.
13 pub inputs: Vec<(Option<String>, i32)>,
14 // +0x10
15 /// The output raw-material template.
16 pub output: Option<String>,
17 // +0x18; +0x170
18 /// Stock group (manager slot of the limit check).
19 pub group: i32,
20 // +0x20
21 /// The UI label key.
22 pub label: String,
23}
24
25/// `ConsumeProduceDescription` (`Template\X.rsc:consumeproduce`).
26///
27/// Original: `FUN_14011e5b0`.
28#[derive(Debug, Clone, Default)]
29pub struct ConsumeProduceDescription {
30 // +0x20
31 /// String table of the recipe labels.
32 pub string_table: Option<String>,
33 // +0x28/+0x30
34 /// The recipes.
35 pub recipes: Vec<ProduceRecipe>,
36 // +0x40
37 /// Work cycles per production.
38 pub cycles: i32,
39 // +0x50
40 /// Work type (tool mesh, PerformWork animation `type + 9` below 11, walk mode).
41 pub work_type: u32,
42 // +0x38 / +0x3c
43 /// The type of a message sent to the building's component of that type after each
44 /// production.
45 pub message_type: u32,
46 /// The value of that message.
47 pub message_value: i32,
48 // +0x44
49 /// Seconds per work cycle.
50 pub duration: f32,
51 // +0x48
52 /// Productions before the workplace is exhausted (−1: never).
53 pub lifetime: i32,
54 // +0x4c
55 /// Initial recipe selector (−1: weighted random).
56 pub selector: i32,
57}
58
59/// Read a [`ConsumeProduceDescription`] from the bytes of its resource.
60pub fn consume_produce_description(data: &[u8]) -> Result<ConsumeProduceDescription> {
61 let (_, value) = crs::parse(data, |r: &mut Reader, h| {
62 if h.type_id != type_id("ConsumeProduceDescription") {
63 return format_err(format!(
64 "expected ConsumeProduceDescription, found {:#010x}",
65 h.type_id
66 ));
67 }
68 r.block(|r, _| {
69 let string_table = r.external_ref()?;
70 let n = r.count()?;
71 let mut recipes = Vec::with_capacity(n);
72 for _ in 0..n {
73 let k = r.count()?;
74 let inputs = (0..k)
75 .map(|_| Ok((r.external_ref()?, r.i32()?)))
76 .collect::<Result<_>>()?;
77 recipes.push(ProduceRecipe {
78 inputs,
79 output: r.external_ref()?,
80 group: r.i32()?,
81 label: r.string()?,
82 });
83 }
84 Ok(ConsumeProduceDescription {
85 string_table,
86 recipes,
87 cycles: r.i32()?,
88 work_type: r.u32()?,
89 message_type: r.u32()?,
90 message_value: r.i32()?,
91 duration: r.f32()?,
92 lifetime: r.i32()?,
93 selector: r.i32()?,
94 })
95 })
96 })?;
97 Ok(value)
98}