Skip to main content

unbanished_game/work/professions/
commands.rs

1//! The player's commands on professions and workplaces, and applying them.
2
3use super::*;
4
5/// A player action on professions, applied by the simulation.
6#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
7pub enum Command {
8    /// ProfessionUI spinner `300 + profession` set to `count`.
9    ///
10    /// Original: `FUN_14007b890`.
11    SetCount {
12        /// The profession.
13        profession: usize,
14        /// The number of workers wanted.
15        count: i32,
16    },
17    /// WorkPlaceUI `0x1450`: one more worker of the workplace's profession.
18    ///
19    /// Original: `FUN_1400918a0`.
20    AddWorker {
21        /// The workplace.
22        entity: usize,
23    },
24    /// WorkPlaceUI `0x1451`: one fewer.
25    ///
26    /// Original: `FUN_140096650`.
27    RemoveWorker {
28        /// The workplace.
29        entity: usize,
30    },
31    /// WorkPlaceUI `0x1452/0x1453`: most workers ± 1.
32    ///
33    /// Original: `FUN_14008cae0`, `FUN_140134fd0`.
34    ChangeMaxWorkers {
35        /// The workplace.
36        entity: usize,
37        /// The change: +1 or −1.
38        delta: i32,
39    },
40    /// The pause button of a building's window (element 6000).
41    ///
42    /// Original: `FUN_1400b7370`, `FUN_1401350d0`.
43    SetEnabled {
44        /// The building.
45        entity: usize,
46        /// Whether it works.
47        enabled: bool,
48    },
49    // slot +0x58
50    /// CropUI's crop list: the field's crop; choosing the first one switches farming on.
51    ///
52    /// Original: `FUN_1400b7df0`.
53    SetCrop {
54        /// The field.
55        entity: usize,
56        /// The crop, or none.
57        crop: Option<usize>,
58    },
59    /// The field's buttons: 0 plant now, 1 harvest now, 2 farming on/off.
60    ///
61    /// Original: `FUN_140120190`.
62    FieldAction {
63        /// The field.
64        entity: usize,
65        /// The button: 0, 1 or 2.
66        action: i32,
67    },
68    // slot +0x48
69    /// PastureUI's slider: the most animals the pasture keeps.
70    ///
71    /// Original: `FUN_1400ba2f0`.
72    SetLimit {
73        /// The pasture.
74        entity: usize,
75        /// The most animals.
76        limit: i32,
77    },
78    /// Demolish a building (the Remove Buildings tool, or the button in its window).
79    ///
80    /// Original: `FUN_140109b40`, `FUN_140123470`.
81    Demolish {
82        /// The building.
83        entity: usize,
84    },
85    /// DestroyUI `0x1771`: the demolition is called off.
86    ///
87    /// Original: `FUN_1401238d0`.
88    CancelDemolition {
89        /// The building.
90        entity: usize,
91    },
92    /// RoadTool's apply: plan a road on a cell.
93    ///
94    /// Original: `FUN_14010a200`, `FUN_1400a1e30`.
95    PlanRoad {
96        /// The cell.
97        cell: i32,
98        /// The road kind.
99        kind: u8,
100        /// The cell type of the road.
101        ty: u8,
102        /// The raw material it is built from, if any.
103        material: Option<usize>,
104    },
105    /// Remove Roads: take a planned road back; a built one gets a removal marker (`marker`, the
106    /// tool's template).
107    ///
108    /// Original: `FUN_140108ab0`, `FUN_1400a1bd0`.
109    ClearRoad {
110        /// The cell.
111        cell: i32,
112        /// The removal marker's template.
113        marker: usize,
114    },
115    // slot +0x10
116    /// A StateMachine message: DestroyComponent's Remove Cancel (0 while torn down), DestroyUI
117    /// `0x1772` (0: rebuild the rubble) and `0x1773` (1: clear it); ImmigrationUI `0x9858` (7: take
118    /// the nomads in) and `0x9859` (8: turn them away).
119    ///
120    /// Original: `FUN_140123940`, `FUN_1401239a0`.
121    StateMessage {
122        /// The entity.
123        entity: usize,
124        /// The message.
125        message: i32,
126    },
127    // slot +0x70
128    /// ControlUI: the `kind` component's control `index`.
129    ///
130    /// Original: `FUN_140060150`.
131    Control {
132        /// The entity.
133        entity: usize,
134        /// The component kind.
135        kind: u32,
136        /// The control's index.
137        index: i32,
138    },
139    // +0x50
140    /// ConsumeProduceUI's product: ConsumeProduceComponent.
141    ///
142    /// Original: `FUN_14005fc90`.
143    SetRecipe {
144        /// The workplace.
145        entity: usize,
146        /// The product.
147        recipe: i32,
148    },
149    // +0x60
150    /// Upgrade a building to `template` (the upgrade button): it is demolished, and the new
151    /// building takes its place.
152    ///
153    /// Original: `FUN_14008c160`, then `FUN_140123470`.
154    Upgrade {
155        /// The building.
156        entity: usize,
157        /// The template of the new building.
158        template: usize,
159    },
160    /// TradeUI's trade button: sell (material, count) and buy (trade good, count).
161    ///
162    /// Original: `FUN_1400868f0`.
163    Exchange {
164        /// The trading post.
165        entity: usize,
166        /// What the town sells: `(raw material, count)`.
167        sell: Vec<(usize, i32)>,
168        /// What the town buys: `(trade good, count)`.
169        buy: Vec<(usize, i32)>,
170    },
171    /// TradeUI's settings on the TradeComponent.
172    Trade {
173        /// The trading post.
174        entity: usize,
175        /// The setting.
176        setting: crate::commerce::trade::TradeSetting,
177    },
178}
179
180impl Workforce {
181    /// Apply a UI command.
182    pub fn apply(
183        &mut self,
184        command: Command,
185        workers: &mut [Worker],
186        professions: &Professions,
187        homes: &BTreeMap<usize, Home>,
188        map: &mut dyn StorageSearchMap,
189        search: &mut dyn Search,
190    ) {
191        match command {
192            Command::SetCount { profession, count } => self.set_count(
193                workers,
194                professions,
195                profession,
196                count,
197                homes,
198                map,
199                search.rng(),
200            ),
201            Command::AddWorker { entity } => {
202                let Some(p) = self.workplaces.get(&entity).and_then(|w| w.profession) else {
203                    return;
204                };
205                let wp = self.free_workplace(p, search.rng());
206                self.hire(
207                    workers,
208                    professions.laborer,
209                    p,
210                    wp,
211                    homes,
212                    map,
213                    search.rng(),
214                );
215            }
216            Command::RemoveWorker { entity } => {
217                if let Some(p) = self.workplaces.get(&entity).and_then(|w| w.profession) {
218                    self.fire(workers, professions.laborer, p);
219                }
220            }
221            // The simulation answers this one with the building's state
222            // ([`crate::town::Sim::enable_workplace`]).
223            Command::SetEnabled { entity, enabled } => {
224                self.set_enabled(workers, entity, enabled, false, search)
225            }
226            Command::ChangeMaxWorkers { entity, delta } => {
227                if let Some(w) = self.workplaces.get(&entity) {
228                    let max = w.max() + delta;
229                    self.set_max_workers(workers, entity, max, search);
230                }
231            }
232            // Handled by the crop field, orchard and pasture (`crate::production::farm`, `crate::production::pasture`).
233            Command::SetCrop { .. }
234            | Command::FieldAction { .. }
235            | Command::SetLimit { .. }
236            | Command::Demolish { .. }
237            | Command::CancelDemolition { .. }
238            | Command::StateMessage { .. }
239            | Command::PlanRoad { .. }
240            | Command::ClearRoad { .. }
241            | Command::Control { .. }
242            | Command::SetRecipe { .. }
243            | Command::Upgrade { .. }
244            | Command::Exchange { .. }
245            | Command::Trade { .. } => {}
246        }
247    }
248}