Skip to main content

unbanished_formats/game/
construction.rs

1//! Placing, building and removing things: placed and fenced structures, roads, bridges, floors, terraforming, building, destroying and clearing.
2
3mod build;
4pub use build::{
5    build_description, clear_area_description, destroy_description, BuildDescription,
6    ClearAreaDescription, DestroyDescription,
7};
8
9use super::*;
10
11/// `CreatePlacedDescription`: how a building is placed; the start structures take their size from
12/// it.
13///
14/// Original: `FUN_14011eda0`, `FUN_1400a7490`.
15#[derive(Debug, Clone)]
16pub struct CreatePlacedDescription {
17    // +0x20
18    /// Not identified yet.
19    pub flags: u32,
20    // +0x30
21    /// Not identified yet.
22    pub values: Vec<u32>,
23    // +0x28
24    /// Not identified yet.
25    pub name: String,
26    // +0x44, +0x48
27    /// The footprint's width in cells.
28    pub width: i32,
29    /// The footprint's height in cells.
30    pub height: i32,
31    // +0x40
32    /// Not identified yet.
33    pub value: i32,
34    // +0x50
35    /// Not identified yet.
36    pub related: Option<String>,
37}
38
39/// Read a [`CreatePlacedDescription`] from the bytes of its resource.
40pub fn create_placed_description(data: &[u8]) -> Result<CreatePlacedDescription> {
41    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
42        if h.type_id != type_id("CreatePlacedDescription") {
43            return format_err(format!(
44                "expected CreatePlacedDescription, found {:#010x}",
45                h.type_id
46            ));
47        }
48        r.block(|r, _| {
49            let flags = r.u32()?;
50            let values = (0..r.count()?).map(|_| r.u32()).collect::<Result<_>>()?;
51            Ok(CreatePlacedDescription {
52                flags,
53                values,
54                name: r.string()?,
55                width: r.i32()?,
56                height: r.i32()?,
57                value: r.i32()?,
58                related: r.external_ref()?,
59            })
60        })
61    })?;
62    Ok(value)
63}
64
65// vtable 0x140166a10; +0x20; +0x28..+0x50; +0x58; +0x5c; +0x60; +0x64; +0x68; +0x70
66/// `CreateFencedDescription`: the fenced tool's (FencedTool) cell-type mask, its six part templates
67/// (shelter, end, corner, straight, long straight, gate), the largest, and smallest, size, the
68/// shelter centred on its side, and the template whose entities may stand in the way.
69///
70/// Original: `FUN_140048250`.
71#[derive(Debug, Clone, Default, PartialEq)]
72pub struct CreateFencedDescription {
73    /// The cell types it may cover.
74    pub mask: u32,
75    /// The part templates: shelter, end, corner, straight, long straight, gate.
76    pub parts: Vec<Option<String>>,
77    /// The largest size.
78    pub max: (i32, i32),
79    /// The smallest size.
80    pub min: (i32, i32),
81    /// Whether the shelter is centred on its side.
82    pub centered: bool,
83    /// The template whose entities may stand in the way.
84    pub clear: Option<String>,
85}
86
87/// Read a [`CreateFencedDescription`] from the bytes of its resource.
88pub fn create_fenced_description(data: &[u8]) -> Result<CreateFencedDescription> {
89    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
90        if h.type_id != type_id("CreateFencedDescription") {
91            return format_err(format!(
92                "expected CreateFencedDescription, found {:#010x}",
93                h.type_id
94            ));
95        }
96        r.block(|r, _| {
97            let mask = r.u32()?;
98            let parts = (0..6).map(|_| r.external_ref()).collect::<Result<_>>()?;
99            Ok(CreateFencedDescription {
100                mask,
101                parts,
102                max: (r.i32()?, r.i32()?),
103                min: (r.i32()?, r.i32()?),
104                centered: r.bool()?,
105                clear: r.external_ref()?,
106            })
107        })
108    })?;
109    Ok(value)
110}
111
112// +0x20; +0x28
113/// `CreateRoadDescription`: the road kind (0 stone, 1 dirt; a lower kind replaces a higher), the
114/// raw material a cell takes (none for dirt).
115///
116/// Original: `FUN_14011f150`.
117#[derive(Debug, Clone, Default, PartialEq, Eq)]
118pub struct CreateRoadDescription {
119    /// The road kind: 0 stone, 1 dirt. A lower kind replaces a higher one.
120    pub kind: u8,
121    /// The raw material a cell takes (none for dirt).
122    pub material: Option<String>,
123}
124
125/// Read a [`CreateRoadDescription`] from the bytes of its resource.
126pub fn create_road_description(data: &[u8]) -> Result<CreateRoadDescription> {
127    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
128        if h.type_id != type_id("CreateRoadDescription") {
129            return format_err(format!(
130                "expected CreateRoadDescription, found {:#010x}",
131                h.type_id
132            ));
133        }
134        r.block(|r, _| {
135            Ok(CreateRoadDescription {
136                kind: r.u8()?,
137                material: r.external_ref()?,
138            })
139        })
140    })?;
141    Ok(value)
142}
143
144// vtable 0x140167f20; +0x20; +0x24; +0x28; +0x2c; +0x30; +0x34; +0x38
145/// `CreateBridgeDescription`: the bridge tool's (BridgeTool) cell-type masks: the cells it spans
146/// (water), its banks, further cells drawn as valid, at least one spanned cell of these, the cells
147/// beside it within (its half width); the part templates (start, 1, 2, 4 and 8 cells long).
148///
149/// Original: `FUN_14011eaa0`.
150#[derive(Debug, Clone, Default, PartialEq, Eq)]
151pub struct CreateBridgeDescription {
152    /// The cell types it spans (water).
153    pub span: u32,
154    /// The cell types of its banks.
155    pub banks: u32,
156    /// Further cell types drawn as valid.
157    pub shown: u32,
158    /// The cell types of which at least one spanned cell is needed.
159    pub needs: u32,
160    /// The cell types allowed beside it.
161    pub sides: u32,
162    /// Its half width, in cells.
163    pub half_width: i32,
164    /// The part templates: the start, then 1, 2, 4 and 8 cells long.
165    pub parts: Vec<Option<String>>,
166}
167
168/// Read a [`CreateBridgeDescription`] from the bytes of its resource.
169pub fn create_bridge_description(data: &[u8]) -> Result<CreateBridgeDescription> {
170    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
171        if h.type_id != type_id("CreateBridgeDescription") {
172            return format_err(format!(
173                "expected CreateBridgeDescription, found {:#010x}",
174                h.type_id
175            ));
176        }
177        r.block(|r, _| {
178            let span = r.u32()?;
179            let shown = r.u32()?;
180            let needs = r.u32()?;
181            let banks = r.u32()?;
182            let sides = r.u32()?;
183            let half_width = r.i32()?;
184            let parts = (0..5).map(|_| r.external_ref()).collect::<Result<_>>()?;
185            Ok(CreateBridgeDescription {
186                span,
187                banks,
188                shown,
189                needs,
190                sides,
191                half_width,
192                parts,
193            })
194        })
195    })?;
196    Ok(value)
197}
198
199// +0x20; +0x28
200/// `FloorDescription`: the picking mesh walked on, added on activation.
201///
202/// Original: `FUN_140124b50`.
203#[derive(Debug, Clone, Default, PartialEq, Eq)]
204pub struct FloorDescription {
205    /// The picking mesh walked on.
206    pub mesh: Option<String>,
207    /// Whether the floor is added on activation.
208    pub active: bool,
209}
210
211/// Read a [`FloorDescription`] from the bytes of its resource.
212pub fn floor_description(data: &[u8]) -> Result<FloorDescription> {
213    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
214        if h.type_id != type_id("FloorDescription") {
215            return format_err(format!(
216                "expected FloorDescription, found {:#010x}",
217                h.type_id
218            ));
219        }
220        r.block(|r, _| {
221            Ok(FloorDescription {
222                mesh: r.external_ref()?,
223                active: r.bool()?,
224            })
225        })
226    })?;
227    Ok(value)
228}
229
230// +0x20; +0x28; +0x30; +0x34
231/// `TerraformDescription`: a tunnel's floor and ceiling picking meshes, each with the cell types
232/// its vertices may change.
233///
234/// Original: `FUN_140058000`.
235#[derive(Debug, Clone, Default, PartialEq, Eq)]
236pub struct TerraformDescription {
237    /// The floor's picking mesh.
238    pub floor: Option<String>,
239    /// The cell types the floor's vertices may change.
240    pub floor_mask: u32,
241    /// The ceiling's picking mesh.
242    pub ceiling: Option<String>,
243    /// The cell types the ceiling's vertices may change.
244    pub ceiling_mask: u32,
245}
246
247/// Read a [`TerraformDescription`] from the bytes of its resource.
248pub fn terraform_description(data: &[u8]) -> Result<TerraformDescription> {
249    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
250        if h.type_id != type_id("TerraformDescription") {
251            return format_err(format!(
252                "expected TerraformDescription, found {:#010x}",
253                h.type_id
254            ));
255        }
256        r.block(|r, _| {
257            Ok(TerraformDescription {
258                floor: r.external_ref()?,
259                floor_mask: r.u32()?,
260                ceiling: r.external_ref()?,
261                ceiling_mask: r.u32()?,
262            })
263        })
264    })?;
265    Ok(value)
266}
267
268/// `CreateRemovalDescription` (`Template\ClearX.rsc:createremoval`): what a RemovalTool drag does
269/// to each cell.
270///
271/// Original: `FUN_14011ef90`, `FUN_140108ab0`.
272#[derive(Debug, Clone, Default)]
273pub struct CreateRemovalDescription {
274    // +0x20
275    /// Mark natural resources for removal.
276    pub resources: bool,
277    // +0x28/+0x30
278    /// The resource templates it marks (empty: every one).
279    pub filters: Vec<String>,
280    // +0x38
281    /// The marker entity's template (`Template\Clear.rsc:resource`).
282    pub template: Option<String>,
283    // +0x40; +0x41; +0x42; +0x43
284    /// Remove roads.
285    pub roads: bool,
286    /// Demolish buildings.
287    pub buildings: bool,
288    /// Cancel.
289    pub cancel: bool,
290    /// Prioritise.
291    pub priority: bool,
292}
293
294/// Read a [`CreateRemovalDescription`] from the bytes of its resource.
295pub fn create_removal_description(data: &[u8]) -> Result<CreateRemovalDescription> {
296    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
297        if h.type_id != type_id("CreateRemovalDescription") {
298            return format_err(format!(
299                "expected CreateRemovalDescription, found {:#010x}",
300                h.type_id
301            ));
302        }
303        r.block(|r, _| {
304            let resources = r.bool()?;
305            let n = r.count()?;
306            let filters = (0..n)
307                .map(|_| Ok(r.external_ref()?.unwrap_or_default()))
308                .collect::<Result<_>>()?;
309            Ok(CreateRemovalDescription {
310                resources,
311                filters,
312                template: r.external_ref()?,
313                roads: r.bool()?,
314                buildings: r.bool()?,
315                cancel: r.bool()?,
316                priority: r.bool()?,
317            })
318        })
319    })?;
320    Ok(value)
321}
322
323/// `CreateDragDescription` (`Template\X.rsc:createdrag`).
324///
325/// Original: `FUN_14011ec20`.
326#[derive(Debug, Clone, Default)]
327pub struct CreateDragDescription {
328    // +0x20
329    /// The cell types the drag may cover, as a mask of type bits.
330    ///
331    /// Original: read by `FUN_1400a4aa0`.
332    pub mask: u32,
333    /// Not identified yet.
334    pub value_28: i32,
335    /// Not identified yet.
336    pub value_2c: i32,
337    /// Not identified yet.
338    pub value_30: i32,
339    /// Not identified yet.
340    pub value_34: i32,
341    // +0x24
342    /// Ignore cell occupants.
343    pub override_occupant: bool,
344    // +0x38
345    /// An external reference.
346    pub reference: Option<String>,
347}
348
349/// Read a [`CreateDragDescription`] from the bytes of its resource.
350pub fn create_drag_description(data: &[u8]) -> Result<CreateDragDescription> {
351    let (_, value) = crs::parse(data, |r: &mut Reader, h| {
352        if h.type_id != type_id("CreateDragDescription") {
353            return format_err(format!(
354                "expected CreateDragDescription, found {:#010x}",
355                h.type_id
356            ));
357        }
358        r.block(|r, _| {
359            Ok(CreateDragDescription {
360                mask: r.u32()?,
361                value_28: r.i32()?,
362                value_2c: r.i32()?,
363                value_30: r.i32()?,
364                value_34: r.i32()?,
365                override_occupant: r.bool()?,
366                reference: r.external_ref()?,
367            })
368        })
369    })?;
370    Ok(value)
371}