Skip to main content

unbanished_game/work/construction/
states.rs

1//! The controllers' state tables (`StateDef`): for each controller, its states in order, the
2//! callback each one runs, and where it goes next.
3
4mod production;
5use production::{
6    ABSORB_RESOURCE, CEMETERY, CLEAR_RESOURCE, CROP_FIELD, HAPPINESS, MARKET_LOCATION, ORCHARD,
7    PASTURE, RESOURCE_PRODUCE, STORAGE_LOCATION,
8};
9
10use super::*;
11
12/// One state of a building's state machine: its callbacks and its transitions.
13pub struct StateDef {
14    /// The state's number.
15    pub id: i8,
16    /// The callback when the state is entered.
17    pub enter: Callback,
18    /// The callback at each scheduled update.
19    pub update: Callback,
20    /// The callback when the state is left.
21    pub exit: Callback,
22    /// The callbacks of transitions: the target state and its callback.
23    pub transitions: &'static [(i8, Callback)],
24}
25
26pub(super) const CLEAR: &[StateDef] = &[
27    StateDef {
28        id: 0,
29        enter: C::ClearEnter0,
30        update: C::ClearUpdate0,
31        exit: C::ClearExit0,
32        transitions: &[(1, C::None), (3, C::None)],
33    },
34    // `FUN_14011ccb0` / `FUN_14011cca0` are thunks of state 0's enter and update.
35    StateDef {
36        id: 0x15,
37        enter: C::ClearEnter0,
38        update: C::ClearUpdate0,
39        exit: C::ClearExit15,
40        transitions: &[(1, C::None), (3, C::None)],
41    },
42    StateDef {
43        id: 1,
44        enter: C::ClearEnter1,
45        update: C::None,
46        exit: C::None,
47        transitions: &[(2, C::None), (3, C::None)],
48    },
49    StateDef {
50        id: 2,
51        enter: C::ClearEnter2,
52        update: C::None,
53        exit: C::None,
54        transitions: &[(3, C::None)],
55    },
56];
57
58pub(super) const WORKPLACE: &[StateDef] = &[
59    StateDef {
60        id: 0,
61        enter: C::WorkPlaceEnter0,
62        update: C::None,
63        exit: C::None,
64        transitions: &[(1, C::None), (3, C::None)],
65    },
66    StateDef {
67        id: 2,
68        enter: C::WorkPlaceEnter2,
69        update: C::None,
70        exit: C::None,
71        transitions: &[(3, C::None)],
72    },
73    StateDef {
74        id: 3,
75        enter: C::WorkPlaceEnter3,
76        update: C::None,
77        exit: C::None,
78        transitions: &[(6, C::None), (4, C::None), (5, C::None)],
79    },
80];
81
82pub(super) const BUILD: &[StateDef] = &[
83    StateDef {
84        id: 1,
85        enter: C::BuildEnter1,
86        update: C::BuildUpdate1,
87        exit: C::None,
88        transitions: &[(8, C::None), (3, C::None)],
89    },
90    StateDef {
91        id: 8,
92        enter: C::BuildEnter8,
93        update: C::BuildUpdate8,
94        exit: C::BuildExit8,
95        transitions: &[(2, C::BuildFinish), (3, C::None)],
96    },
97    StateDef {
98        id: 2,
99        enter: C::None,
100        update: C::None,
101        exit: C::None,
102        transitions: &[(3, C::None)],
103    },
104];
105
106pub(super) const RESIDENCE: &[StateDef] = &[
107    StateDef {
108        id: 2,
109        enter: C::ResidenceEnter2,
110        // `FUN_14012ee40` runs in `Agents::update` (`life::update_home`).
111        update: C::None,
112        exit: C::None,
113        transitions: &[(3, C::None)],
114    },
115    StateDef {
116        id: 3,
117        enter: C::ResidenceEnter3,
118        update: C::None,
119        exit: C::None,
120        transitions: &[(6, C::None), (4, C::None), (5, C::None)],
121    },
122];
123
124/// The state messages (`FUN_140122860` in 6, `FUN_140122770` / `FUN_140122670` in 7,
125/// `FUN_140121190` in 4 and 5) are [`crate::town::Sim::destroy_message`].
126pub(super) const DESTROY: &[StateDef] = &[
127    StateDef {
128        id: 3,
129        enter: C::DestroyEnter3,
130        update: C::None,
131        exit: C::None,
132        transitions: &[(4, C::None), (6, C::None)],
133    },
134    StateDef {
135        id: 6,
136        enter: C::DestroyEnter6,
137        update: C::DestroyUpdate6,
138        exit: C::DestroyExit6,
139        transitions: &[(7, C::None)],
140    },
141    StateDef {
142        id: 7,
143        enter: C::None,
144        update: C::None,
145        exit: C::None,
146        transitions: &[(0, C::None), (8, C::None), (5, C::None)],
147    },
148    StateDef {
149        id: 4,
150        enter: C::DestroyEnter4,
151        update: C::DestroyUpdate4,
152        exit: C::None,
153        transitions: &[(5, C::None), (2, C::None)],
154    },
155    StateDef {
156        id: 5,
157        enter: C::DestroyEnter5,
158        update: C::DestroyUpdate5,
159        exit: C::None,
160        transitions: &[(0, C::None), (2, C::None), (8, C::None), (7, C::None)],
161    },
162];
163
164pub(super) const WELL: &[StateDef] = &[
165    StateDef {
166        id: 2,
167        enter: C::WellEnter2,
168        update: C::None,
169        exit: C::None,
170        transitions: &[(3, C::None)],
171    },
172    StateDef {
173        id: 3,
174        enter: C::WellEnter3,
175        update: C::None,
176        exit: C::None,
177        transitions: &[(6, C::None), (4, C::None), (5, C::None)],
178    },
179];
180
181/// The state messages (6 in 0x10, 7 and 8 in 0x11) are
182/// [`crate::town::Sim::immigration_message`].
183pub(super) const IMMIGRATION: &[StateDef] = &[
184    StateDef {
185        id: 2,
186        enter: C::ImmigrationEnter2,
187        update: C::ImmigrationUpdate2,
188        exit: C::None,
189        transitions: &[(3, C::None), (0xf, C::None)],
190    },
191    StateDef {
192        id: 0xf,
193        enter: C::ImmigrationEnterF,
194        update: C::ImmigrationUpdateF,
195        exit: C::None,
196        transitions: &[(3, C::None), (0x10, C::None), (2, C::None)],
197    },
198    StateDef {
199        id: 0x10,
200        enter: C::ImmigrationEnter10,
201        update: C::ImmigrationUpdate10,
202        exit: C::None,
203        transitions: &[(3, C::ImmigrationLeave), (0x11, C::None)],
204    },
205    StateDef {
206        id: 0x11,
207        enter: C::ImmigrationEnter11,
208        update: C::ImmigrationUpdate11,
209        exit: C::None,
210        transitions: &[(3, C::ImmigrationLeave), (2, C::None)],
211    },
212];
213
214/// The state messages (9 and 0xb in 0x12, 0xa in 0x13) are
215/// [`crate::town::Sim::trade_message`].
216/// `FUN_14012b000` (`FUN_140036a50`, `FUN_140036b70`): state 2 only.
217pub(super) const MULTI_OBJECT: &[StateDef] = &[StateDef {
218    id: 2,
219    enter: C::MultiObjectEnter2,
220    update: C::None,
221    exit: C::None,
222    transitions: &[(3, C::None)],
223}];
224
225pub(super) const TRADE: &[StateDef] = &[
226    StateDef {
227        id: 2,
228        enter: C::TradeEnter2,
229        update: C::TradeUpdate2,
230        exit: C::None,
231        transitions: &[(3, C::TradeLeave), (0x12, C::None)],
232    },
233    StateDef {
234        id: 0x12,
235        enter: C::TradeEnter12,
236        update: C::None,
237        exit: C::None,
238        transitions: &[(3, C::TradeLeave), (0x13, C::None), (2, C::None)],
239    },
240    StateDef {
241        id: 0x13,
242        enter: C::TradeEnter13,
243        update: C::TradeUpdate13,
244        exit: C::TradeExit13,
245        transitions: &[(3, C::TradeLeave), (2, C::None)],
246    },
247];
248
249pub(super) const ATTENDANCE: &[StateDef] = &[
250    StateDef {
251        id: 2,
252        enter: C::AttendanceEnter2,
253        update: C::None,
254        exit: C::AttendanceExit2,
255        transitions: &[(3, C::None)],
256    },
257    StateDef {
258        id: 3,
259        enter: C::AttendanceEnter3,
260        update: C::None,
261        exit: C::None,
262        transitions: &[(6, C::None), (4, C::None), (5, C::None)],
263    },
264];
265
266pub(super) const HEALER: &[StateDef] = &[
267    StateDef {
268        id: 2,
269        enter: C::HealerEnter2,
270        update: C::None,
271        exit: C::None,
272        transitions: &[(3, C::None), (0xd, C::None)],
273    },
274    StateDef {
275        id: 3,
276        enter: C::HealerEnter3,
277        update: C::None,
278        exit: C::None,
279        transitions: &[(6, C::None), (4, C::None), (5, C::None)],
280    },
281];
282
283impl Controller {
284    /// The states of a controller.
285    pub fn table(self) -> &'static [StateDef] {
286        match self {
287            Controller::ClearArea => CLEAR,
288            Controller::WorkPlace => WORKPLACE,
289            Controller::Build => BUILD,
290            Controller::Residence => RESIDENCE,
291            Controller::ClearResource => CLEAR_RESOURCE,
292            Controller::ResourceProduce => RESOURCE_PRODUCE,
293            Controller::Attendance => ATTENDANCE,
294            Controller::Healer => HEALER,
295            Controller::Happiness => HAPPINESS,
296            Controller::CropField => CROP_FIELD,
297            Controller::Orchard => ORCHARD,
298            Controller::Pasture => PASTURE,
299            Controller::AbsorbResource => ABSORB_RESOURCE,
300            Controller::StorageLocation => STORAGE_LOCATION,
301            Controller::Cemetery => CEMETERY,
302            Controller::MarketLocation => MARKET_LOCATION,
303            Controller::Destroy => DESTROY,
304            Controller::Well => WELL,
305            Controller::Immigration => IMMIGRATION,
306            Controller::Trade => TRADE,
307            Controller::MultiObject => MULTI_OBJECT,
308        }
309    }
310
311    // +0x80
312    /// The initial state the first controller gives (ClearArea `0`, Build `1`).
313    ///
314    /// Original: `FUN_140003730`, `FUN_14002fc50`.
315    pub fn initial(self) -> i8 {
316        match self {
317            Controller::Build => 1,
318            _ => 0,
319        }
320    }
321
322    /// Interface slot `+0`: the state's index.
323    pub fn find(self, state: i8) -> Option<usize> {
324        self.table().iter().position(|d| d.id == state)
325    }
326}