banished_game/work/
control.rs1use crate::town::Sim;
9use crate::work::construction::{run, Callbacks};
10use crate::work::jobs::Jobs;
11
12pub mod kind {
14 pub const CROP_FIELD: u32 = 0xa3cc_d302;
16 pub const ORCHARD: u32 = 0xa981_9b71;
18 pub const PASTURE: u32 = 0x66ae_21ad;
20 pub const TRADE: u32 = 0x9292_c700;
22 pub const ATTENDANCE: u32 = 0x5146_364d;
24 pub const FORESTER: u32 = 0xb18e_acda;
26 pub const CONSUME_PRODUCE: u32 = 0x1935_bdbb;
28 pub const FISHING: u32 = 0x9d9a_f271;
30 pub const HUNT: u32 = 0xacc2_7fa4;
32 pub const MARKET: u32 = 0x4c8f_7637;
34 pub const HERBALIST: u32 = 0xca1a_33a3;
36}
37
38impl Sim<'_, '_> {
39 pub fn control(&mut self, jobs: &mut Jobs, e: usize, kind: u32, index: i32) {
42 match kind {
43 kind::CROP_FIELD => self.field_action(e, index),
44 kind::ORCHARD => self.orchard_action(e, index),
45 kind::PASTURE => {
47 if self.pasture_query(e, index) & 1 != 0 {
48 self.pasture_action(jobs, e, index);
49 }
50 }
51 kind::TRADE => self.toggle_trade_work(jobs, e),
52 kind::ATTENDANCE => {
55 let on = !self.workforce.workplaces.get(&e).is_some_and(|w| w.enabled);
56 self.enable_workplace(e, on);
57 if !on {
58 self.evict(e);
59 }
60 }
61 kind::FORESTER => self.forester_toggle(jobs, e, index),
62 kind::CONSUME_PRODUCE | kind::FISHING | kind::HUNT | kind::MARKET | kind::HERBALIST => {
63 self.produce_toggle(jobs, e)
64 }
65 _ => {}
66 }
67 }
68
69 pub(crate) fn produce_toggle(&mut self, jobs: &mut Jobs, e: usize) {
72 let Some(site) = self.sites.get(&e) else {
73 return;
74 };
75 if site.machine.index < 0 {
76 run(self, jobs, e, false, false);
77 }
78 let site = &self.sites[&e];
79 let disabled = site.produce.as_ref().is_some_and(|p| p.disabled);
80 match site.machine.state {
81 0xd => self.machine(e).request(0xe),
82 0xe if !disabled => self.machine(e).request(0xd),
83 _ => {}
84 }
85 run(self, jobs, e, false, false);
86 }
87
88 fn forester_toggle(&mut self, jobs: &mut Jobs, e: usize, index: i32) {
91 let Some(f) = self.sites.get_mut(&e).and_then(|s| s.forester.as_mut()) else {
92 return;
93 };
94 let before = f.plant || f.cut;
95 match index {
96 1 => f.plant = !f.plant,
97 2 => f.cut = !f.cut,
98 _ => {}
99 }
100 let after = f.plant || f.cut;
101 if before != after {
102 self.produce_toggle(jobs, e);
103 }
104 }
105}