unbanished_game/work/construction/site.rs
1//! What a building site holds: its template, production, workers, decals, piles and the work it
2//! offers, and the town's list of sites (`Construction`).
3
4mod progress;
5mod work;
6pub use progress::Construction;
7pub use work::{Attendance, BuildWork, ClearWork, Gather, PickupWork, Pile};
8
9use super::*;
10
11/// The parts of a building's descriptions construction reads.
12#[derive(Debug, Clone, Default)]
13pub struct SiteTemplate {
14 /// `Template\X.rsc:resource`.
15 pub reference: String,
16 /// The controllers in component order.
17 pub controllers: Vec<Controller>,
18 // +0x20; +0x30
19 /// The materials to build it: `(count, raw material)`.
20 pub materials: Vec<(i32, usize)>,
21 /// The building work needed, in work units.
22 pub work: i32,
23 // +0x2c
24 /// ClearAreaDescription: present, flatten/stamp flag.
25 pub clear: Option<unbanished_formats::game::ClearAreaDescription>,
26 // +0x24
27 /// WorkDescription (most workers once built).
28 pub max_workers: i32,
29 // +0x34; +0x30; +0x24
30 /// Scale the most workers by the footprint's area over this many cells (from the work
31 /// description).
32 pub per_area: Option<i32>,
33 // +0x20; +0x28; +0x2c
34 /// Whether the player may change the most workers in the building's window.
35 pub adjustable: bool,
36 /// The fewest workers the player may set.
37 pub min_jobs: i32,
38 /// The most workers the player may set.
39 pub max_jobs: i32,
40 // +0x20
41 /// WorkPlaceDescription.
42 pub profession: Option<usize>,
43 /// ConsumeProduceDescription, resolved.
44 pub produce: Option<ProduceTemplate>,
45 /// StorageDescription: accepted flags, capacity, per footprint cell.
46 pub storage: Option<(u32, i32, bool)>,
47 /// ModelDescription meshes per variant (construction stages, the last finished).
48 pub meshes: Vec<usize>,
49 /// ResidenceDescription.
50 pub residence: Option<unbanished_formats::game::ResidenceDescription>,
51 /// AttendanceDescription capacity.
52 pub attendance: Option<i32>,
53 /// HerbalistDescription (gatherer, herbalist), resolved.
54 pub herbalist: Option<crate::production::herbalist::HerbalistTemplate>,
55 // +0x28
56 /// RadiusDescription.
57 pub radius: Option<i32>,
58 // +0x20
59 /// AutoPickupDescription.
60 pub auto_pickup: Option<bool>,
61 /// ForesterDescription, resolved.
62 pub forester: Option<crate::production::forester::ForesterTemplate>,
63 /// HappinessDescription.
64 pub happiness: Option<unbanished_formats::game::HappinessDescription>,
65 /// FishingDescription, resolved.
66 pub fishing: Option<crate::production::fishing::FishingTemplate>,
67 /// HuntDescription.
68 pub hunt: Option<unbanished_formats::game::HuntDescription>,
69 /// CropFieldDescription.
70 pub crop_field: Option<crate::production::farm::CropFieldTemplate>,
71 /// OrchardDescription.
72 pub orchard: Option<crate::production::orchard::OrchardTemplate>,
73 // +0x20
74 /// PastureDescription: the stock slot of its products' limit.
75 pub pasture: Option<i32>,
76 /// DiseaseDescription (fields, orchards, pastures).
77 pub disease: Option<unbanished_formats::game::DiseaseDescription>,
78 /// CemeteryDescription's tombstone template and its TombstoneDescription.
79 pub cemetery: Option<crate::people::cemetery::CemeteryTemplate>,
80 /// MarketDescription.
81 pub market: Option<unbanished_formats::game::MarketDescription>,
82 // ModelComponent +0x5c
83 /// StatusIconDescription (presentation), and the finished model's height its icons stand on
84 /// (`ModelComponent`).
85 pub status_icon: Option<unbanished_formats::game::StatusIconDescription>,
86 /// The height of the status icon above the building, if it has one.
87 pub icon_height: Option<f32>,
88 /// DestroyDescription, resolved.
89 pub destroy: Option<crate::work::destroy::DestroyTemplate>,
90 /// FireDescription (`Template\Common.rsc:fire*`).
91 pub fire: Option<unbanished_formats::game::FireDescription>,
92 /// ImmigrationDescription, resolved.
93 pub immigration: Option<crate::people::immigration::ImmigrationTemplate>,
94 /// TradeDescription, resolved.
95 pub trade: Option<crate::commerce::trade::TradeTemplate>,
96 // +0x68; +0x6c
97 /// TradeDescription /: the visitors' disease risk trading brings, and its months.
98 pub trade_risk: Option<(f32, i32)>,
99 /// The pile template of each BuildDescription material (the refunds' piles).
100 pub material_templates: Vec<Option<usize>>,
101}
102
103/// A resolved [`unbanished_formats::game::ConsumeProduceDescription`].
104#[derive(Debug, Clone, Default)]
105pub struct ProduceTemplate {
106 /// The recipes.
107 pub recipes: Vec<Recipe>,
108 // +0x40
109 /// Work cycles per production.
110 pub cycles: i32,
111 // +0x50
112 /// Work type (tool mesh, animation, walk).
113 pub work_type: u32,
114 // +0x38/+0x3c
115 /// A message to the building's component of that type after a production.
116 pub message: (u32, i32),
117 // +0x44
118 /// Seconds per cycle.
119 pub duration: f32,
120 // +0x48
121 /// Productions before exhaustion (−1 never).
122 pub lifetime: i32,
123 // +0x4c
124 /// Initial selector.
125 pub selector: i32,
126}
127
128/// A production alternative: inputs (raw material, count), the output pile template and its
129/// raw material, and the stock group the limit check reads.
130#[derive(Debug, Clone, Default)]
131pub struct Recipe {
132 /// What it uses: `(raw material, count)`.
133 pub inputs: Vec<(usize, i32)>,
134 /// The template of the output pile.
135 pub output: usize,
136 /// The raw material of the output.
137 pub output_material: usize,
138 /// The stock group whose limit is checked.
139 pub group: i32,
140}
141
142// +0x48; +0x50; +0x58; +0x68
143/// ConsumeProduceComponent state (switched off, selector, weights, productions so far).
144#[derive(Debug, Clone, Default)]
145pub struct ProduceState {
146 /// Whether production is switched off.
147 pub disabled: bool,
148 /// The recipe chosen in the window.
149 pub selector: i32,
150 /// The weight of each recipe, for choosing what to make.
151 pub weights: Vec<f32>,
152 /// The productions so far.
153 pub produced: i32,
154}
155
156// +0x58; +0x68; +0x48; +0x4c
157/// `ConsumeProduce` work state: the worker at each use point, each output point's state (0 free, 1
158/// a production is under way, 2 holds output), every output point was taken, work cycles towards
159/// the next production.
160#[derive(Debug, Clone, Default)]
161pub struct ProduceWork {
162 /// The worker at each use point.
163 pub workers: Vec<Option<usize>>,
164 /// The state of each output point: 0 free, 1 a production is under way, 2 holds output.
165 pub points: Vec<u8>,
166 /// Whether every output point was taken.
167 pub full: bool,
168 /// The work cycles towards the next production.
169 pub cycles: i32,
170}
171
172// vtable 0x140169400; +0x30; +0x28; +0x2c
173/// DecalComponent (0x38 bytes, serialized alpha, material): setup takes the description's alpha;
174/// entity event 1 (the ClearArea's state 2) and the build's state 8 make it opaque; a collapse
175/// switches to the DestroyDescription's material. Each change re-adds the map decal. See
176/// docs/DECALS.md.
177///
178/// Original: `FUN_140120ca0`, `FUN_14011ccc0`, `FUN_14010cfb0`, `FUN_140122aa0`, `FUN_140120c30`.
179#[derive(Debug, Clone, Copy, Default, PartialEq)]
180pub struct Decal {
181 // +0x30
182 /// is 1.
183 pub opaque: bool,
184 // +0x28
185 /// Index into the description's materials.
186 pub material: i32,
187}
188
189/// One building under construction (and, once built, its WorkComponent).
190#[derive(Debug, Clone)]
191pub struct Site {
192 /// The building's entity.
193 pub entity: usize,
194 /// Index into [`Construction::templates`].
195 pub template: usize,
196 /// The building's state machine.
197 pub machine: Machine,
198 // +0x48; +0x4c
199 /// BuildComponent progress, stages.
200 pub progress: i32,
201 /// The construction stages.
202 pub stages: i32,
203 // +0x28; +0x2c
204 /// WorkComponent registered work id (−1) and kept priority.
205 pub work_id: i32,
206 /// The priority to keep for its work, if any.
207 pub work_priority: Option<i32>,
208 /// The building's StorageComponent (index into the town's storages).
209 pub storage: usize,
210 /// State 2 reached.
211 pub complete: bool,
212 // +0x48
213 /// ClearAreaComponent: objects on the footprint when clearing began.
214 pub clear_total: i32,
215 /// The entity was destroyed during a state callback; the site goes once the machine has run.
216 ///
217 /// Original: `FUN_1400c8370`.
218 pub destroyed: bool,
219 // +0x48
220 /// ConsumeProduceComponent (ResourceProduceComponent's for the others).
221 pub produce: Option<ProduceState>,
222 /// AutoPickupComponent.
223 pub auto_pickup: Option<crate::production::herbalist::AutoPickup>,
224 /// ForesterComponent's toggles.
225 pub forester: Option<crate::production::forester::Forester>,
226 // +0x28
227 /// HappinessComponent: registered.
228 pub happiness_registered: bool,
229 /// FishingComponent.
230 pub fishing: Option<crate::production::fishing::Fishing>,
231 // +0x50
232 /// HuntComponent: the hunting time gathered.
233 pub hunt_time: Option<i64>,
234 /// CropFieldComponent.
235 pub crop_field: Option<crate::production::farm::CropField>,
236 /// OrchardComponent.
237 pub orchard: Option<crate::production::orchard::Orchard>,
238 // +0x48
239 /// MultiObjectComponent: the entities placed with this one (a pasture's shelter and fences), in
240 /// order.
241 pub parts: Vec<usize>,
242 // +0x58; +0x4c
243 /// MultiObjectComponent: the stages its construction counts (a bridge's cells, a fenced area's
244 /// border cells); `BuildComponent::EnterGather` takes it for.
245 pub multi_stages: Option<i32>,
246 /// DiseaseComponent.
247 pub disease: Option<crate::disasters::blight::Blight>,
248 // +0x48
249 /// CemeteryComponent: the grave cells.
250 ///
251 /// Original: `FUN_140047b60`.
252 pub graves: Vec<i32>,
253 /// DestroyComponent.
254 pub destroy: Option<crate::work::destroy::Destroy>,
255 /// FireComponent.
256 pub fire: Option<crate::disasters::fire::Fire>,
257 /// ImmigrationComponent.
258 pub immigration: Option<crate::people::immigration::Immigration>,
259 /// TradeComponent.
260 pub trade: Option<crate::commerce::trade::Trade>,
261 /// The ResidenceComponent while the residence is out of the town's (its states 3 to 7).
262 pub home: Option<crate::people::life::Home>,
263 // +0x28; +0x30; +0x20
264 /// UIComponent (bytes, the UIDescription's): the window's remembered settings
265 /// (`DialogInstance::SaveSettings`), empty until it is first closed.
266 pub ui_settings: Vec<u8>,
267}
268
269impl SiteTemplate {
270 /// The default number of workers of a building of `footprint` cells.
271 ///
272 /// Original: `WorkComponent::MaxWorkers`, `FUN_140133a90`.
273 pub fn default_max_workers(&self, footprint: (i32, i32)) -> i32 {
274 let Some(area) = self.per_area else {
275 return self.max_workers;
276 };
277 let n = self.max_workers;
278 // `DAT_14017884c` = 1.0.
279 let scaled = ((footprint.0 * footprint.1) as f32 / area as f32 * n as f32 + 1.0) as i32;
280 if scaled <= n {
281 scaled.max(1)
282 } else {
283 n
284 }
285 }
286}